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.

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