|
5 | 5 | import tarfile
|
6 | 6 | import tensorflow as tf
|
7 | 7 | import zipfile
|
8 |
| -import time |
| 8 | +from datetime import datetime |
9 | 9 | from Xlib import display
|
10 | 10 | import cv2
|
11 | 11 | import yaml
|
12 | 12 |
|
13 |
| - |
14 | 13 | from collections import defaultdict
|
15 | 14 | from io import StringIO
|
16 | 15 | #from PIL import Image
|
|
19 | 18 | sys.path.append('../tensorflow_models/research/slim')
|
20 | 19 | sys.path.append('../tensorflow_models/research/object_detection')
|
21 | 20 |
|
22 |
| -from utils import label_map_util |
23 |
| -from utils import visualization_utils as vis_util |
24 |
| - |
25 |
| -from stuff.helper import FPS |
| 21 | +from stuff.helper import FPS, Visualizer |
26 | 22 | from stuff.input import ScreenInput, VideoInput
|
27 | 23 |
|
28 | 24 | # Load config values from config.obj_detect.sample.yml (as default values) updated by optional user-specific config.obj_detect.yml
|
|
50 | 46 | # Path to frozen detection graph. This is the actual model that is used for the object detection.
|
51 | 47 | PATH_TO_CKPT = '../' + cfg['model_name'] + '/frozen_inference_graph.pb'
|
52 | 48 |
|
53 |
| -# List of the strings that is used to add correct label for each box. |
54 |
| -PATH_TO_LABELS = os.path.join('../tensorflow_models/research/object_detection/data', 'mscoco_label_map.pbtxt') |
55 | 49 |
|
56 |
| -NUM_CLASSES = 90 |
57 | 50 |
|
58 | 51 | # ## Download Model
|
59 | 52 | MODEL_FILE = cfg['model_name'] + cfg['model_dl_file_format']
|
|
79 | 72 | od_graph_def.ParseFromString(serialized_graph)
|
80 | 73 | tf.import_graph_def(od_graph_def, name='')
|
81 | 74 |
|
82 |
| -# ## Loading label map |
83 |
| -# Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine |
84 |
| -label_map = label_map_util.load_labelmap(PATH_TO_LABELS) |
85 |
| -categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True) |
86 |
| -category_index = label_map_util.create_category_index(categories) |
87 |
| - |
88 | 75 | # # Detection
|
89 | 76 | PATH_TO_TEST_IMAGES_DIR = 'test_images'
|
90 | 77 | TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]
|
|
107 | 94 | # TODO: Usually FPS calculation lives in a separate thread. As is now, the interval is a minimum value for each iteration.
|
108 | 95 | fps = FPS(cfg['fps_interval']).start()
|
109 | 96 |
|
110 |
| - windowPlacedYet = False |
| 97 | + vis = Visualizer(cfg['visualizer_enabled']) |
111 | 98 |
|
112 | 99 | while(input.isActive()):
|
| 100 | + |
| 101 | +# startTime=datetime.now() |
| 102 | + |
113 | 103 | ret, image_np = input.getImage()
|
114 | 104 | if not ret:
|
115 |
| - print("No frames grabbed from input (anymore)! Exit.") |
| 105 | + print("No frames grabbed from input (anymore). Exit.") |
116 | 106 | break
|
117 | 107 |
|
118 |
| -# image_np_bgr = np.array(ImageGrab.grab(bbox=(0,0,600,600))) # grab(bbox=(10,10,500,500)) or just grab() |
119 |
| -# image_np = cv2.cvtColor(image_np_bgr, cv2.COLOR_BGR2RGB) |
| 108 | +# timeElapsed=datetime.now()-startTime |
| 109 | +# print('1 Time elpased (hh:mm:ss.ms) {}'.format(timeElapsed)) |
| 110 | +# startTime=datetime.now() |
120 | 111 |
|
121 | 112 | # for image_path in TEST_IMAGE_PATHS:
|
122 | 113 | # image = Image.open(image_path)
|
|
130 | 121 | (boxes, scores, classes, num) = sess.run(
|
131 | 122 | [detection_boxes, detection_scores, detection_classes, num_detections],
|
132 | 123 | feed_dict={image_tensor: image_np_expanded})
|
133 |
| - # Visualization of the results of a detection. |
134 |
| - vis_util.visualize_boxes_and_labels_on_image_array( |
135 |
| - image_np, |
136 |
| - np.squeeze(boxes), |
137 |
| - np.squeeze(classes).astype(np.int32), |
138 |
| - np.squeeze(scores), |
139 |
| - category_index, |
140 |
| - use_normalized_coordinates=True, |
141 |
| - line_thickness=8) |
142 |
| - |
143 |
| - cv2.imshow('object detection', image_np) # alternatively as 2nd param: cv2.resize(image_np, (800, 600))) |
144 |
| - if cv2.waitKey(1) & 0xFF == ord('q'): |
145 |
| - break |
146 |
| - if not windowPlacedYet: |
147 |
| - cv2.moveWindow('object detection', (int)(screen.width/3), (int)(screen.height/3)) |
148 |
| - windowPlacedYet = True |
| 124 | + |
| 125 | + ret = vis.show(image_np, boxes, classes, scores) |
| 126 | + if not ret: |
| 127 | + print("User asked to quit. Exit") |
| 128 | + break |
149 | 129 |
|
150 | 130 | fps.update()
|
151 | 131 |
|
|
154 | 134 | print('[INFO] approx. FPS: {:.2f}'.format(fps.fps()))
|
155 | 135 |
|
156 | 136 | input.cleanup()
|
157 |
| -cv2.destroyAllWindows() |
| 137 | +vis.cleanup() |
0 commit comments