Keine Beschreibung

yolo.py 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import cv2
  2. import numpy as np
  3. import argparse
  4. import time
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument('--webcam', help="True/False", default=False)
  7. parser.add_argument('--play_video', help="Tue/False", default=False)
  8. parser.add_argument('--play_flask', help="Tue/False", default=False)
  9. parser.add_argument('--image', help="Tue/False", default=False)
  10. parser.add_argument('--video_path', help="Path of video file", default="videos/car_on_road.mp4")
  11. parser.add_argument('--image_path', help="Path of image to detect objects", default="Images/bicycle.jpg")
  12. parser.add_argument('--verbose', help="To print statements", default=True)
  13. args = parser.parse_args()
  14. #Load yolo
  15. def load_yolo():
  16. #net = cv2.dnn.readNet("yolov3-tiny.weights", "yolov3-tiny.cfg")
  17. net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
  18. classes = []
  19. with open("coco.names", "r") as f:
  20. classes = [line.strip() for line in f.readlines()]
  21. output_layers = [layer_name for layer_name in net.getUnconnectedOutLayersNames()]
  22. colors = np.random.uniform(0, 255, size=(len(classes), 3))
  23. return net, classes, colors, output_layers
  24. def load_image(img_path):
  25. # image loading
  26. img = cv2.imread(img_path)
  27. img = cv2.resize(img, None, fx=0.4, fy=0.4)
  28. height, width, channels = img.shape
  29. return img, height, width, channels
  30. def start_webcam():
  31. cap = cv2.VideoCapture(0)
  32. return cap
  33. def display_blob(blob):
  34. '''
  35. Three images each for RED, GREEN, BLUE channel
  36. '''
  37. for b in blob:
  38. for n, imgb in enumerate(b):
  39. cv2.imshow(str(n), imgb)
  40. def detect_objects(img, net, outputLayers):
  41. blob = cv2.dnn.blobFromImage(img, scalefactor=0.00392, size=(320, 320), mean=(0, 0, 0), swapRB=True, crop=False)
  42. net.setInput(blob)
  43. outputs = net.forward(outputLayers)
  44. return blob, outputs
  45. def get_box_dimensions(outputs, height, width):
  46. boxes = []
  47. confs = []
  48. class_ids = []
  49. for output in outputs:
  50. for detect in output:
  51. scores = detect[5:]
  52. class_id = np.argmax(scores)
  53. conf = scores[class_id]
  54. if conf > 0.3:
  55. center_x = int(detect[0] * width)
  56. center_y = int(detect[1] * height)
  57. w = int(detect[2] * width)
  58. h = int(detect[3] * height)
  59. x = int(center_x - w/2)
  60. y = int(center_y - h / 2)
  61. boxes.append([x, y, w, h])
  62. confs.append(float(conf))
  63. class_ids.append(class_id)
  64. return boxes, confs, class_ids
  65. def draw_labels(boxes, confs, colors, class_ids, classes, img):
  66. indexes = cv2.dnn.NMSBoxes(boxes, confs, 0.5, 0.4)
  67. font = cv2.FONT_HERSHEY_PLAIN
  68. for i in range(len(boxes)):
  69. if i in indexes:
  70. x, y, w, h = boxes[i]
  71. label = str(classes[class_ids[i]])
  72. color = colors[i]
  73. cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
  74. cv2.putText(img, label, (x, y - 5), font, 3, color, 3)
  75. imS = cv2.resize(img, (960, 540))
  76. cv2.imshow("Image", imS)
  77. def draw_labels2(boxes, confs, colors, class_ids, classes, img):
  78. indexes = cv2.dnn.NMSBoxes(boxes, confs, 0.4, 0.4)
  79. font = cv2.FONT_HERSHEY_PLAIN
  80. for i in range(len(boxes)):
  81. if i in indexes:
  82. x, y, w, h = boxes[i]
  83. label = str(classes[class_ids[i]])
  84. color = colors[i]
  85. cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
  86. cv2.putText(img, label, (x, y - 5), font, 3, color, 3)
  87. return img
  88. def image_detect(img_path):
  89. model, classes, colors, output_layers = load_yolo()
  90. image, height, width, channels = load_image(img_path)
  91. blob, outputs = detect_objects(image, model, output_layers)
  92. boxes, confs, class_ids = get_box_dimensions(outputs, height, width)
  93. draw_labels(boxes, confs, colors, class_ids, classes, image)
  94. while True:
  95. key = cv2.waitKey(1)
  96. if key == 27:
  97. break
  98. def webcam_detect():
  99. model, classes, colors, output_layers = load_yolo()
  100. cap = start_webcam()
  101. while True:
  102. _, frame = cap.read()
  103. height, width, channels = frame.shape
  104. blob, outputs = detect_objects(frame, model, output_layers)
  105. boxes, confs, class_ids = get_box_dimensions(outputs, height, width)
  106. draw_labels(boxes, confs, colors, class_ids, classes, frame)
  107. key = cv2.waitKey(1)
  108. if key == 27:
  109. break
  110. cap.release()
  111. def start_video(video_path):
  112. model, classes, colors, output_layers = load_yolo()
  113. cap = cv2.VideoCapture(video_path)
  114. while True:
  115. _, frame = cap.read()
  116. height, width, channels = frame.shape
  117. blob, outputs = detect_objects(frame, model, output_layers)
  118. boxes, confs, class_ids = get_box_dimensions(outputs, height, width)
  119. draw_labels(boxes, confs, colors, class_ids, classes, frame)
  120. k = cv2.waitKey(50) & 0xFF
  121. if k == 27:
  122. cv2.destroyAllWindows()
  123. break
  124. cap.release()
  125. from flask import Flask, render_template, Response
  126. app = Flask(__name__)
  127. def find_camera(id):
  128. '''
  129. cameras = ['rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp',
  130. 'rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp']
  131. '''
  132. cameras = ['rtsp://admin:@Unv123456@192.168.10.252:554/unicast/c1/s1/live']
  133. return cameras[int(id)]
  134. # for cctv camera use rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp' instead of camera
  135. # for webcam use zero(0)
  136. def gen_frames(camera_id):
  137. cam = find_camera(camera_id)
  138. video_path = cam
  139. model, classes, colors, output_layers = load_yolo()
  140. cap = cv2.VideoCapture(video_path)
  141. while True:
  142. _, frame = cap.read()
  143. if frame is not None:
  144. height, width, channels = frame.shape
  145. blob, outputs = detect_objects(frame, model, output_layers)
  146. boxes, confs, class_ids = get_box_dimensions(outputs, height, width)
  147. frame = draw_labels2(boxes, confs, colors, class_ids, classes, frame)
  148. ret, buffer = cv2.imencode('.jpg', frame)
  149. frame = buffer.tobytes()
  150. yield (b'--frame\r\n'
  151. b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
  152. cap.release()
  153. @app.route('/video_feed/<string:id>/', methods=["GET"])
  154. def video_feed(id):
  155. """Video streaming route. Put this in the src attribute of an img tag."""
  156. return Response(gen_frames(id),
  157. mimetype='multipart/x-mixed-replace; boundary=frame')
  158. if __name__ == '__main__':
  159. webcam = args.webcam
  160. video_play = args.play_video
  161. flask_play = args.play_flask
  162. image = args.image
  163. if webcam:
  164. if args.verbose:
  165. print('---- Starting Web Cam object detection ----')
  166. webcam_detect()
  167. if video_play:
  168. video_path = args.video_path
  169. if args.verbose:
  170. print('Opening '+video_path+" .... ")
  171. start_video(video_path)
  172. if image:
  173. image_path = args.image_path
  174. if args.verbose:
  175. print("Opening "+image_path+" .... ")
  176. image_detect(image_path)
  177. if flask_play:
  178. app.run(debug=True, port=9024)
  179. cv2.destroyAllWindows()