From 2c37d4f9f05c5920807276bad911df200f947eaa Mon Sep 17 00:00:00 2001 From: Daniel M Date: Mon, 12 Oct 2020 23:30:04 +1100 Subject: [PATCH 1/2] Created camera.py, which is an adaptation of video.py to process an IP camera stream. --- YOLOv3_TF2/camera.py | 124 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 YOLOv3_TF2/camera.py diff --git a/YOLOv3_TF2/camera.py b/YOLOv3_TF2/camera.py new file mode 100644 index 0000000..885cf2a --- /dev/null +++ b/YOLOv3_TF2/camera.py @@ -0,0 +1,124 @@ +""" +Amendment: +Updated video.py to camera.py, this sample is tailored to accept an RTSP camera stream. +Adjustments were made so that the frame grab only captures the most recent frame for detection processing. + +Originally Written by: Rahmad Sadli +Website : https://machinelearningspace.com + +I finally made this program simple and readable +Hopefully, this program will help some beginners like me to understand better object detection. +If you want to redistribute it, just keep the author's name. + +In oder to execute this program, you need to install TensorFlow 2.0 and opencv 4.x + +For more details about how this program works. I explained well about it, just click the link below: +https://machinelearningspace.com/the-beginners-guide-to-implementing-yolo-v3-in-tensorflow-2-0-part-1/ + +Credit to: +Ayoosh Kathuria who shared his great work using pytorch, really appreaciated it. +https://blog.paperspace.com/how-to-implement-a-yolo-object-detector-in-pytorch/ + +""" + + +import tensorflow as tf + +from utils import load_class_names, output_boxes, draw_outputs, resize_image +import cv2 +import time + +from yolov3 import YOLOv3Net + +#If you don't have enough GPU hardware device available in your machine, uncomment the following three lines: +physical_devices = tf.config.experimental.list_physical_devices('GPU') +assert len(physical_devices) > 0, "Not enough GPU hardware devices available" +tf.config.experimental.set_memory_growth(physical_devices[0], True) + + +model_size = (416, 416,3) +num_classes = 80 +class_name = './data/coco.names' +max_output_size = 100 +max_output_size_per_class= 20 +iou_threshold = 0.5 +confidence_threshold = 0.5 + + +cfgfile = 'cfg/yolov3.cfg' +weightfile = 'weights/yolov3_weights.tf' + +def main(): + + model = YOLOv3Net(cfgfile,model_size,num_classes) + + model.load_weights(weightfile) + + class_names = load_class_names(class_name) + + + + win_name = 'Yolov3 detection' + cv2.namedWindow(win_name) + + # Specify the camera url. + # For camera, just change the camera URL to match your IP camera RTSP stream or MPEG stream. + cap = cv2.VideoCapture("rtsp://user:password@172.168.50.208:554/cam/realmonitor?channel=1&subtype=1") + frame_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), + cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) + + try: + while True: + start = time.time() + cap.grab() # Grab the most recent frame from the camera stream + ret, frame = cap.read() # Read it into a frame buffer + if not ret: + break + + resized_frame = tf.expand_dims(frame, 0) + resized_frame = resize_image(resized_frame, (model_size[0],model_size[1])) + + pred = model.predict(resized_frame) + + boxes, scores, classes, nums = output_boxes( \ + pred, model_size, + max_output_size=max_output_size, + max_output_size_per_class=max_output_size_per_class, + iou_threshold=iou_threshold, + confidence_threshold=confidence_threshold) + + img = draw_outputs(frame, boxes, scores, classes, nums, class_names) + cv2.imshow(win_name, img) + + stop = time.time() + + seconds = stop - start + # print("Time taken : {0} seconds".format(seconds)) + + # Calculate frames per second + fps = 1 / seconds + print("Estimated frames per second : {0}".format(fps)) + + key = cv2.waitKey(1) & 0xFF + if key == ord('q'): + break + if key == 27: + break + + # Adjust frame rate + #if fps > 30: + # fps = fps * 0.5 + # cap.set(cv2.CAP_PROP_FPS, int(fps)) + # print("Changing frame rate to: {0}".format(int(fps))) + #else: + # cap.set(cv2.CAP_PROP_FPS, 10) + # print("Changing frame rate to: {0}".format(int(fps))) + + finally: + cv2.destroyAllWindows() + cap.release() + print('Detections have been performed successfully.') + + +if __name__ == '__main__': + main() From 672199eef626d1c9d809fc6b4b8fc052253b93e8 Mon Sep 17 00:00:00 2001 From: Daniel M Date: Mon, 12 Oct 2020 23:31:49 +1100 Subject: [PATCH 2/2] Added test comment for new camera.py example. --- YOLOv3_TF2/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/YOLOv3_TF2/README.md b/YOLOv3_TF2/README.md index ca14619..048e273 100644 --- a/YOLOv3_TF2/README.md +++ b/YOLOv3_TF2/README.md @@ -60,4 +60,8 @@ to the weights folder. python video.py +5. Test an rtsp IP camera stream. Update the URL in camera.py to test. + + python camera.py + Good luck