Welcome to Raspbot repository
6.6 Tensorflow object recognition
3.1.4 Tensorflow object recognition
TensorFlow is an open source software library that uses data flow graphs for numerical calculations.
Features
1. High flexibility
2. True Portability
3. Connect scientific research and products
4. Differentiate automatically
5. Support multi-language
6. Performance optimization
Code path:
/home/pi/Yahboom_Project/Raspbot/3.AI Vision course/05.Object recognition/Object recognition.ipynb
|
|
|
|
|
|
print("dict['Name']: ", category_index[1]['name']) |
# Main Thread
t_start = time.time()
fps = 0
with detection_graph.as_default():
with tf.compat.v1.Session(graph=detection_graph) as sess:
while True:
ret, frame = cap.read()
# frame = cv2.flip(frame, -1) # Flip camera vertically
# frame = cv2.resize(frame,(320,240))
##############
image_np_expanded = np.expand_dims(frame, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# print('Running detection..')
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# print('Done. Visualizing..')
vis_utils.visualize_boxes_and_labels_on_image_array(
frame,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
for i in range(0, 10):
if scores[0][i] >= 0.5:
print(category_index[int(classes[0][i])]['name'])
##############
fps = fps + 1
mfps = fps / (time.time() - t_start)
cv2.putText(frame, "FPS " + str(int(mfps)), (10,10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
image_widget.value = bgr8_to_jpeg(frame)
k = cv2.waitKey(3000) & 0xff
if k == 27: # press 'ESC' to quit
break
cap.release()
cv2.destroyAllWindows() |
This program runs on JupyterLab with low frame rate, but you can try python programs on a Raspberry Pi desktop, effect will become better.