with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
while True:
ret, image_np = cap.read()
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
(boxes, scores, classes, num_detections) = sess.run([boxes, scores, classes, num_detections], feed_dict={image_tensor: image_np_expanded})
boxes = np.squeeze(boxes)
scores = np.squeeze(scores)
classes = np.squeeze(classes).astype(np.int32)
for i in range(boxes.shape[0]):
if scores is None or scores[i] > 0.6:
class_name = category_index[classes[i]]['name']
box = tuple(boxes[i].tolist())
cv2.rectangle(image_np, box[1:3], box[3:1:-1], (255,0,0), 3)
cv2.putText(image_np, class_name, box[1:3], cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
cv2.imshow('Object Detection', cv2.resize(image_np, (800, 600)))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
firebase.put('/object_detection', image_np)
This code sets up a TensorFlow Object Detection model to detect objects in real-time from a webcam feed. It then sends the detected objects to Firebase using the firebase.put() method. You can modify the code to suit your specific needs, such as changing the path to the TensorFlow model or adjusting the detection threshold.