You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
11 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. #!/usr/bin/python3
  2. import pickle
  3. import threading
  4. import sys
  5. import cv2
  6. import os
  7. import numpy as np
  8. from utils import label_map_util
  9. from utils import visualization_utils as vis_util
  10. if sys.platform == "win32":
  11. import tensorflow as tf
  12. from distutils.version import StrictVersion
  13. if StrictVersion(tf.__version__) < StrictVersion('1.12.0'):
  14. raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')
  15. else:
  16. import psutil
  17. import json
  18. import base64
  19. from PIL import Image
  20. from io import BytesIO
  21. switch = 1
  22. import socket
  23. # This is needed since the notebook is stored in the object_detection folder.
  24. sys.path.append("..")
  25. import time
  26. from object_detection.utils import ops as utils_ops
  27. # What model to download.
  28. encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
  29. #MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17' #not even worth trying
  30. #MODEL_NAME = "ssd_inception_v2_coco_2017_11_17" # not bad and fast
  31. MODEL_NAME = "rfcn_resnet101_coco_11_06_2017" # WORKS BEST BUT takes 4 times longer per image
  32. #MODEL_NAME = "faster_rcnn_resnet101_coco_11_06_2017" # too slow
  33. #MODEL_NAME = "ssd_resnet101_v1_fpn_shared_box_predictor_oid_512x512_sync_2019_01_20"
  34. MODEL_FILE = MODEL_NAME + '.tar.gz'
  35. DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
  36. # Path to frozen detection graph. This is the actual model that is used for the object detection.
  37. PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'
  38. # List of the strings that is used to add correct label for each box.
  39. PATH_TO_LABELS = os.path.join('object_detection/data', 'mscoco_label_map.pbtxt')
  40. category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
  41. if sys.platform == "win32":
  42. detection_graph = tf.Graph()
  43. with detection_graph.as_default():
  44. od_graph_def = tf.GraphDef()
  45. with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
  46. serialized_graph = fid.read()
  47. od_graph_def.ParseFromString(serialized_graph)
  48. tf.import_graph_def(od_graph_def, name='')
  49. def load_image_into_numpy_array(image):
  50. (im_width, im_height) = image.size
  51. return np.array(image.getdata()).reshape(
  52. (im_height, im_width, 3)).astype(np.uint8)
  53. # For the sake of simplicity we will use only 2 images:
  54. # image1.jpg
  55. # image2.jpg
  56. # If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
  57. PATH_TO_TEST_IMAGES_DIR = 'object_detection/test_images'
  58. TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(3, 6) ]
  59. # Size, in inches, of the output images.
  60. sess = 0
  61. data = {"gpu_temp":"10C","gpu_load":"15%","cpu_temp":"47C","cpu_load":"15%","mem_temp":"NaN","mem_load":"17%","fan_speed":"10000RPM"}
  62. def get_temps():
  63. global data
  64. if not sys.platform == "win32":
  65. temps = psutil.sensors_temperatures()
  66. data["cpu_temp"] = str(int(temps["dell_smm"][0][1]))+"°C"
  67. data["cpu_load"] = str(psutil.cpu_percent())+"%"
  68. data["mem_load"] = str(dict(psutil.virtual_memory()._asdict())["percent"])+"%"
  69. data["fan_speed"] = str(psutil.sensors_fans()["dell_smm"][0][1])+"RPM"
  70. def run_inference_for_single_image(image, graph):
  71. global switch
  72. global sess
  73. with graph.as_default():
  74. if(switch):
  75. sess = tf.Session()
  76. switch = 0
  77. # Get handles to input and output tensors
  78. ops = tf.get_default_graph().get_operations()
  79. all_tensor_names = {output.name for op in ops for output in op.outputs}
  80. tensor_dict = {}
  81. for key in [
  82. 'num_detections', 'detection_boxes', 'detection_scores',
  83. 'detection_classes', 'detection_masks'
  84. ]:
  85. tensor_name = key + ':0'
  86. if tensor_name in all_tensor_names:
  87. tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
  88. tensor_name)
  89. if 'detection_masks' in tensor_dict:
  90. # The following processing is only for single image
  91. detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
  92. detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
  93. # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
  94. real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
  95. detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
  96. detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
  97. detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
  98. detection_masks, detection_boxes, image.shape[1], image.shape[2])
  99. detection_masks_reframed = tf.cast(
  100. tf.greater(detection_masks_reframed, 0.5), tf.uint8)
  101. # Follow the convention by adding back the batch dimension
  102. tensor_dict['detection_masks'] = tf.expand_dims(
  103. detection_masks_reframed, 0)
  104. image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
  105. # Run inference
  106. output_dict = sess.run(tensor_dict,
  107. feed_dict={image_tensor: image})
  108. # all outputs are float32 numpy arrays, so convert types as appropriate
  109. output_dict['num_detections'] = int(output_dict['num_detections'][0])
  110. output_dict['detection_classes'] = output_dict[
  111. 'detection_classes'][0].astype(np.int64)
  112. output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
  113. output_dict['detection_scores'] = output_dict['detection_scores'][0]
  114. if 'detection_masks' in output_dict:
  115. output_dict['detection_masks'] = output_dict['detection_masks'][0]
  116. return output_dict
  117. kill = True
  118. def listener(port=8385):
  119. serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  120. serversocket.bind((socket.gethostname(), port))
  121. serversocket.listen(5)
  122. while kill:
  123. serversocket.accept()
  124. print('Bye!')
  125. cut = [115, 100, 400, 150]
  126. cut_send = [0, 0, 0, 0]
  127. img_counter = 0
  128. socket_switch = True
  129. thread = threading.Thread(target=listener)
  130. thread.start()
  131. if sys.platform == "win32":
  132. with detection_graph.as_default():
  133. sess = tf.Session()
  134. cam = cv2.VideoCapture(0)
  135. else:
  136. cam = cv2.VideoCapture('debug_data/amb_1.mp4')
  137. with open("debug_data/frame_data.pkl","rb") as pkl_file:
  138. frame_data = pickle.load(pkl_file)
  139. switch = 0
  140. get_temps()
  141. amb_center = {'x': (400 + 550)/2, 'y': (115+215)/2}
  142. reps = -1
  143. reps_vid = 0
  144. while 1:
  145. ret,image = cam.read()
  146. reps_vid += 1
  147. if not sys.platform == "win32" and not reps_vid % 2 == 0:
  148. continue
  149. reps += 1
  150. try: # Kavşak
  151. t1 = time.time()
  152. image_np = image
  153. image_np_expanded = np.expand_dims(image_np, axis=0)
  154. if sys.platform == "win32":
  155. output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
  156. else:
  157. output_dict = frame_data[reps]
  158. height, width, channels = image_np.shape
  159. out_dict = {'detection_boxes': [], 'detection_classes': [], 'detection_scores': []}
  160. for i in output_dict['detection_classes']:
  161. cont = False
  162. if i in [3, 6, 8]: # Car, bus, truck
  163. index = np.where(output_dict['detection_classes'] == i)[0][0]
  164. score = output_dict['detection_scores'][index]
  165. if score > 0.3:
  166. if not any((output_dict['detection_boxes'][index] == b).all() for b in out_dict['detection_boxes']):
  167. avg_x = (output_dict['detection_boxes'][index][0] + output_dict['detection_boxes'][index][2])/2
  168. avg_y = (output_dict['detection_boxes'][index][1] + output_dict['detection_boxes'][index][3])/2
  169. for box in out_dict['detection_boxes']:
  170. avg_box_x = (box[0] + box[2])/2
  171. avg_box_y = (box[1] + box[3])/2
  172. if abs(avg_x-avg_box_x) < 0.1 and abs(avg_y-avg_box_y) < 0.1:
  173. cont = True
  174. continue
  175. out_dict['detection_classes'].append(i)
  176. out_dict['detection_boxes'].append(output_dict['detection_boxes'][index])
  177. out_dict['detection_scores'].append(output_dict['detection_scores'][index])
  178. out_dict['detection_classes'] = np.array(out_dict['detection_classes'])
  179. out_dict['detection_boxes'] = np.array(out_dict['detection_boxes'])
  180. out_dict['detection_scores'] = np.array(out_dict['detection_scores'])
  181. print(len(out_dict['detection_classes']), ' cars.')
  182. vis_util.visualize_boxes_and_labels_on_image_array(
  183. image_np,
  184. out_dict['detection_boxes'],
  185. out_dict['detection_classes'],
  186. out_dict['detection_scores'],
  187. category_index,
  188. instance_masks=out_dict.get('detection_masks'),
  189. use_normalized_coordinates=True,
  190. line_thickness=8,
  191. min_score_thresh=0.3
  192. )
  193. cv2.imshow('frame', image_np)
  194. ex_c = [27, ord("q"), ord("Q")]
  195. if cv2.waitKey(1) & 0xFF in ex_c:
  196. break
  197. t2 = time.time()
  198. print("time taken for {}".format(t2-t1))
  199. if not sys.platform == "win32" and t2-t1 < 0.1:
  200. time.sleep(0.1-(t2-t1))
  201. send_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  202. if socket_switch:
  203. try:
  204. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  205. client_socket.settimeout(0.1)
  206. client_socket.connect(('127.0.0.1', 8485))
  207. connection = client_socket.makefile('wb')
  208. socket_switch = False
  209. except:
  210. socket_switch = True
  211. continue
  212. try:
  213. crop_img = send_image.copy(order='C')
  214. crop_img = Image.fromarray(crop_img, "RGB")
  215. buffered = BytesIO()
  216. crop_img.save(buffered, format="JPEG")
  217. img = base64.b64encode(buffered.getvalue()).decode("ascii")
  218. lens = [len(send_image), 0, len(send_image[0])]
  219. for i in range(0,len(cut), 2):
  220. if cut[i] < 0:
  221. cut_send[i] = lens[i] + cut[i]
  222. cut_send[i+1] = abs(cut[i])-abs(cut[i+1])
  223. client_socket.sendall(json.dumps({"image_full":img,"image_sizes":{"x":cut_send[2],"y":cut_send[0],"width":cut_send[3],"height":cut_send[1]},"load":data}).encode('gbk')+b"\n")
  224. img_counter += 1
  225. except:
  226. socket_switch = True
  227. if img_counter % 10 == 0:
  228. get_temps()
  229. except Exception as e:
  230. if hasattr(e, 'message'):
  231. print(e.message)
  232. else:
  233. print(e)
  234. break
  235. if not socket_switch:
  236. client_socket.sendall(b"Bye\n")
  237. cam.release()
  238. cv2.destroyAllWindows()
  239. cam.release()
  240. kill = False
  241. thread.join()