| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- import cv2
- import numpy as np
- import argparse
- import time
- parser = argparse.ArgumentParser()
- parser.add_argument('--webcam', help="True/False", default=False)
- parser.add_argument('--play_video', help="Tue/False", default=False)
- parser.add_argument('--play_flask', help="Tue/False", default=False)
- parser.add_argument('--image', help="Tue/False", default=False)
- parser.add_argument('--video_path', help="Path of video file", default="videos/car_on_road.mp4")
- parser.add_argument('--image_path', help="Path of image to detect objects", default="Images/bicycle.jpg")
- parser.add_argument('--verbose', help="To print statements", default=True)
- args = parser.parse_args()
- #Load yolo
- def load_yolo():
- #net = cv2.dnn.readNet("yolov3-tiny.weights", "yolov3-tiny.cfg")
- net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
- classes = []
- with open("coco.names", "r") as f:
- classes = [line.strip() for line in f.readlines()]
- output_layers = [layer_name for layer_name in net.getUnconnectedOutLayersNames()]
- colors = np.random.uniform(0, 255, size=(len(classes), 3))
- return net, classes, colors, output_layers
- def load_image(img_path):
- # image loading
- img = cv2.imread(img_path)
- img = cv2.resize(img, None, fx=0.4, fy=0.4)
- height, width, channels = img.shape
- return img, height, width, channels
- def start_webcam():
- cap = cv2.VideoCapture(0)
- return cap
- def display_blob(blob):
- '''
- Three images each for RED, GREEN, BLUE channel
- '''
- for b in blob:
- for n, imgb in enumerate(b):
- cv2.imshow(str(n), imgb)
- def detect_objects(img, net, outputLayers):
- blob = cv2.dnn.blobFromImage(img, scalefactor=0.00392, size=(320, 320), mean=(0, 0, 0), swapRB=True, crop=False)
- net.setInput(blob)
- outputs = net.forward(outputLayers)
- return blob, outputs
- def get_box_dimensions(outputs, height, width):
- boxes = []
- confs = []
- class_ids = []
- for output in outputs:
- for detect in output:
- scores = detect[5:]
- class_id = np.argmax(scores)
- conf = scores[class_id]
- if conf > 0.3:
- center_x = int(detect[0] * width)
- center_y = int(detect[1] * height)
- w = int(detect[2] * width)
- h = int(detect[3] * height)
- x = int(center_x - w/2)
- y = int(center_y - h / 2)
- boxes.append([x, y, w, h])
- confs.append(float(conf))
- class_ids.append(class_id)
- return boxes, confs, class_ids
- def draw_labels(boxes, confs, colors, class_ids, classes, img):
- indexes = cv2.dnn.NMSBoxes(boxes, confs, 0.5, 0.4)
- font = cv2.FONT_HERSHEY_PLAIN
- for i in range(len(boxes)):
- if i in indexes:
- x, y, w, h = boxes[i]
- label = str(classes[class_ids[i]])
- color = colors[i]
- cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
- cv2.putText(img, label, (x, y - 5), font, 3, color, 3)
- imS = cv2.resize(img, (960, 540))
- cv2.imshow("Image", imS)
- def draw_labels2(boxes, confs, colors, class_ids, classes, img):
- indexes = cv2.dnn.NMSBoxes(boxes, confs, 0.4, 0.4)
- font = cv2.FONT_HERSHEY_PLAIN
- for i in range(len(boxes)):
- if i in indexes:
- x, y, w, h = boxes[i]
- label = str(classes[class_ids[i]])
- color = colors[i]
- cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
- cv2.putText(img, label, (x, y - 5), font, 3, color, 3)
- return img
- def image_detect(img_path):
- model, classes, colors, output_layers = load_yolo()
- image, height, width, channels = load_image(img_path)
- blob, outputs = detect_objects(image, model, output_layers)
- boxes, confs, class_ids = get_box_dimensions(outputs, height, width)
- draw_labels(boxes, confs, colors, class_ids, classes, image)
- while True:
- key = cv2.waitKey(1)
- if key == 27:
- break
- def webcam_detect():
- model, classes, colors, output_layers = load_yolo()
- cap = start_webcam()
- while True:
- _, frame = cap.read()
- height, width, channels = frame.shape
- blob, outputs = detect_objects(frame, model, output_layers)
- boxes, confs, class_ids = get_box_dimensions(outputs, height, width)
- draw_labels(boxes, confs, colors, class_ids, classes, frame)
- key = cv2.waitKey(1)
- if key == 27:
- break
- cap.release()
- def start_video(video_path):
- model, classes, colors, output_layers = load_yolo()
- cap = cv2.VideoCapture(video_path)
- while True:
- _, frame = cap.read()
- height, width, channels = frame.shape
- blob, outputs = detect_objects(frame, model, output_layers)
- boxes, confs, class_ids = get_box_dimensions(outputs, height, width)
- draw_labels(boxes, confs, colors, class_ids, classes, frame)
- k = cv2.waitKey(50) & 0xFF
- if k == 27:
- cv2.destroyAllWindows()
- break
- cap.release()
- from flask import Flask, render_template, Response
- app = Flask(__name__)
- def find_camera(id):
- '''
- cameras = ['rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp',
- 'rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp']
- '''
- cameras = ['rtsp://admin:@Unv123456@192.168.10.252:554/unicast/c1/s1/live']
- return cameras[int(id)]
- # for cctv camera use rtsp://username:password@ip_address:554/user=username_password='password'_channel=channel_number_stream=0.sdp' instead of camera
- # for webcam use zero(0)
- def gen_frames(camera_id):
- cam = find_camera(camera_id)
- video_path = cam
- model, classes, colors, output_layers = load_yolo()
- cap = cv2.VideoCapture(video_path)
- while True:
- _, frame = cap.read()
- if frame is not None:
- height, width, channels = frame.shape
- blob, outputs = detect_objects(frame, model, output_layers)
- boxes, confs, class_ids = get_box_dimensions(outputs, height, width)
- frame = draw_labels2(boxes, confs, colors, class_ids, classes, frame)
- ret, buffer = cv2.imencode('.jpg', frame)
- frame = buffer.tobytes()
- yield (b'--frame\r\n'
- b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') # concat frame one by one and show result
- cap.release()
- @app.route('/video_feed/<string:id>/', methods=["GET"])
- def video_feed(id):
- """Video streaming route. Put this in the src attribute of an img tag."""
- return Response(gen_frames(id),
- mimetype='multipart/x-mixed-replace; boundary=frame')
- if __name__ == '__main__':
- webcam = args.webcam
- video_play = args.play_video
- flask_play = args.play_flask
- image = args.image
- if webcam:
- if args.verbose:
- print('---- Starting Web Cam object detection ----')
- webcam_detect()
- if video_play:
- video_path = args.video_path
- if args.verbose:
- print('Opening '+video_path+" .... ")
- start_video(video_path)
- if image:
- image_path = args.image_path
- if args.verbose:
- print("Opening "+image_path+" .... ")
- image_detect(image_path)
- if flask_play:
- app.run(debug=True, port=9024)
- cv2.destroyAllWindows()
|