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.

216 lines
8.3 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
  1. #!/usr/bin/python3
  2. import numpy as np
  3. import os
  4. import sys
  5. import tensorflow as tf
  6. import cv2
  7. from distutils.version import StrictVersion
  8. import socket
  9. from utils import label_map_util
  10. from utils import visualization_utils as vis_util
  11. import psutil
  12. import json
  13. import base64
  14. from PIL import Image
  15. from io import BytesIO
  16. import psutil
  17. switch = 1
  18. import io
  19. import socket
  20. import struct
  21. import time
  22. import pickle
  23. import zlib
  24. # This is needed since the notebook is stored in the object_detection folder.
  25. sys.path.append("..")
  26. import time
  27. from object_detection.utils import ops as utils_ops
  28. if StrictVersion(tf.__version__) < StrictVersion('1.12.0'):
  29. raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')
  30. # What model to download.
  31. encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
  32. MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17' #not even worth trying
  33. #MODEL_NAME="ssd_inception_v2_coco_11_06_2017" # not bad and fast
  34. #MODEL_NAME="rfcn_resnet101_coco_11_06_2017" # WORKS BEST BUT takes 4 times longer per image
  35. #MODEL_NAME = "faster_rcnn_resnet101_coco_11_06_2017" # too slow
  36. MODEL_FILE = MODEL_NAME + '.tar.gz'
  37. DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
  38. # Path to frozen detection graph. This is the actual model that is used for the object detection.
  39. PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'
  40. # List of the strings that is used to add correct label for each box.
  41. PATH_TO_LABELS = os.path.join('object_detection/data', 'mscoco_label_map.pbtxt')
  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. category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
  50. def load_image_into_numpy_array(image):
  51. (im_width, im_height) = image.size
  52. return np.array(image.getdata()).reshape(
  53. (im_height, im_width, 3)).astype(np.uint8)
  54. # For the sake of simplicity we will use only 2 images:
  55. # image1.jpg
  56. # image2.jpg
  57. # If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
  58. PATH_TO_TEST_IMAGES_DIR = 'object_detection/test_images'
  59. TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(3, 6) ]
  60. # Size, in inches, of the output images.
  61. sess = 0
  62. switch = 1
  63. data = {"gpu_temp":"10C","gpu_load":"15%","cpu_temp":"47C","cpu_load":"15%","mem_temp":"NaN","mem_load":"17%","fan_speed":"10000RPM"}
  64. def get_temps():
  65. global data
  66. temps = psutil.sensors_temperatures()
  67. data["cpu_temp"] = str(int(temps["dell_smm"][0][1]))+"°C"
  68. data["cpu_load"] = str(psutil.cpu_percent())+"%"
  69. data["mem_load"] = str(dict(psutil.virtual_memory()._asdict())["percent"])+"%"
  70. data["fan_speed"] = str(psutil.sensors_fans()["dell_smm"][0][1])+"RPM"
  71. def run_inference_for_single_image(image, graph):
  72. global switch
  73. global sess
  74. with graph.as_default():
  75. if(switch):
  76. sess = tf.Session()
  77. switch = 0
  78. # Get handles to input and output tensors
  79. ops = tf.get_default_graph().get_operations()
  80. all_tensor_names = {output.name for op in ops for output in op.outputs}
  81. tensor_dict = {}
  82. for key in [
  83. 'num_detections', 'detection_boxes', 'detection_scores',
  84. 'detection_classes', 'detection_masks'
  85. ]:
  86. tensor_name = key + ':0'
  87. if tensor_name in all_tensor_names:
  88. tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
  89. tensor_name)
  90. if 'detection_masks' in tensor_dict:
  91. # The following processing is only for single image
  92. detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
  93. detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
  94. # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
  95. real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
  96. detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
  97. detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
  98. detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
  99. detection_masks, detection_boxes, image.shape[1], image.shape[2])
  100. detection_masks_reframed = tf.cast(
  101. tf.greater(detection_masks_reframed, 0.5), tf.uint8)
  102. # Follow the convention by adding back the batch dimension
  103. tensor_dict['detection_masks'] = tf.expand_dims(
  104. detection_masks_reframed, 0)
  105. image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
  106. # Run inference
  107. output_dict = sess.run(tensor_dict,
  108. feed_dict={image_tensor: image})
  109. # all outputs are float32 numpy arrays, so convert types as appropriate
  110. output_dict['num_detections'] = int(output_dict['num_detections'][0])
  111. output_dict['detection_classes'] = output_dict[
  112. 'detection_classes'][0].astype(np.int64)
  113. output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
  114. output_dict['detection_scores'] = output_dict['detection_scores'][0]
  115. if 'detection_masks' in output_dict:
  116. output_dict['detection_masks'] = output_dict['detection_masks'][0]
  117. return output_dict
  118. cut=[-175,-1,-175,-1]
  119. cut_send = [0,0,0,0]
  120. a = 1
  121. img_counter = 0
  122. socket_switch = True
  123. cam = cv2.VideoCapture(0)
  124. with detection_graph.as_default():
  125. sess = tf.Session()
  126. switch = 0
  127. get_temps()
  128. while 1:
  129. if(True):
  130. try:
  131. ret,image = cam.read()
  132. image_np = image[cut[0]:cut[1],cut[2]:cut[3]]
  133. #image_np = image_np[int(r[1]):int(r[1]+r[3]),int(r[0]):int(r[0]+r[2])]
  134. # the array based representation of the image will be used later in order to prepare the
  135. # result image with boxes and labels on it.
  136. # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
  137. image_np_expanded = np.expand_dims(image_np, axis=0)
  138. t1 = time.time()
  139. # Actual detection.
  140. output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
  141. # Visualization of the results of a detection.
  142. vis_util.visualize_boxes_and_labels_on_image_array(
  143. image_np,
  144. output_dict['detection_boxes'],
  145. output_dict['detection_classes'],
  146. output_dict['detection_scores'],
  147. category_index,
  148. instance_masks=output_dict.get('detection_masks'),
  149. use_normalized_coordinates=True,
  150. line_thickness=8)
  151. image[cut[0]:cut[1],cut[2]:cut[3]] = image_np
  152. send_image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
  153. cv2.imshow("Cam",image)
  154. cv2.imshow("Cut",image_np)
  155. if socket_switch:
  156. try:
  157. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  158. client_socket.connect(('127.0.0.1', 8485))
  159. connection = client_socket.makefile('wb')
  160. socket_switch = False
  161. except:
  162. socket_switch=True
  163. continue
  164. try:
  165. crop_img = send_image.copy(order='C')
  166. crop_img = Image.fromarray(crop_img,"RGB")
  167. buffered = BytesIO()
  168. crop_img.save(buffered, format="JPEG")
  169. img = base64.b64encode(buffered.getvalue()).decode("ascii")
  170. 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")
  171. img_counter += 1
  172. except:
  173. socket_switch=True
  174. if img_counter % 10 ==0:
  175. get_temps()
  176. t2 = time.time()
  177. print("time taken for {}".format(t2-t1))
  178. ex_c = [27, ord("q"), ord("Q")]
  179. if cv2.waitKey(1) & 0xFF in ex_c:
  180. break
  181. except KeyboardInterrupt:
  182. if not socket_switch:
  183. client_socket.sendall(b"Bye\n")
  184. cam.release()
  185. exit(0)
  186. cv2.destroyAllWindows()
  187. cam.release()