Created
June 8, 2020 15:32
-
-
Save elbruno/ecdc4d14068d570438b99e1240d21c6e to your computer and use it in GitHub Desktop.
WebCamObjectDetectionMobileNetSSD.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bruno Capuano 2020 | |
# display the camera feed using OpenCV | |
# display FPS | |
# load MobileNetSSD object detector trained with COCO Dataset (20 classes) | |
# analyze each camera frame using MobileNet | |
# enable disable obj detection pressing D key | |
import numpy as np | |
import time | |
import cv2 | |
import os | |
def initMobileNetSSD(): | |
global classesMobileNetSSD, colorsMobileNetSSD, net | |
classesMobileNetSSD = ["background", "aeroplane", "bicycle", "bird", "boat", | |
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable", | |
"dog", "horse", "motorbike", "person", "pottedplant", "sheep", | |
"sofa", "train", "tvmonitor"] | |
colorsMobileNetSSD = np.random.uniform(0, 255, size=(len(classesMobileNetSSD), 3)) | |
net = cv2.dnn.readNetFromCaffe(prototxtFile, modelFile) | |
def analyzeFrame(frame, displayBoundingBox = True, displayClassName = True, displayConfidence = True): | |
global H, W | |
# init | |
if W is None or H is None: | |
(H, W) = frame.shape[:2] | |
if net is None: | |
initMobileNetSSD() | |
mobileNetSSDImgSize = (300, 300) | |
blob = cv2.dnn.blobFromImage(cv2.resize(frame, mobileNetSSDImgSize), 0.007843, mobileNetSSDImgSize, 127.5) | |
net.setInput(blob) | |
detections = net.forward() | |
for i in np.arange(0, detections.shape[2]): | |
confidence = detections[0, 0, i, 2] | |
if confidence > confidenceDef: | |
idx = int(detections[0, 0, i, 1]) | |
box = detections[0, 0, i, 3:7] * np.array([W, H, W, H]) | |
(startX, startY, endX, endY) = box.astype("int") | |
if(displayBoundingBox): | |
cv2.rectangle(frame, (startX, startY), (endX, endY), colorsMobileNetSSD[idx], 2) | |
if(displayClassName and displayConfidence): | |
label = "{}: {:.2f}%".format(classesMobileNetSSD[idx], confidence * 100) | |
y = startY - 15 if startY - 15 > 15 else startY + 15 | |
cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colorsMobileNetSSD[idx], 2) | |
elif (displayClassName): | |
label = str(f"{classesMobileNetSSD[idx]}") | |
y = startY - 15 if startY - 15 > 15 else startY + 15 | |
cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colorsMobileNetSSD[idx], 2) | |
# Camera Settings | |
camera_Width = 640 # 1024 # 1280 # 640 | |
camera_Heigth = 480 # 780 # 960 # 480 | |
frameSize = (camera_Width, camera_Heigth) | |
video_capture = cv2.VideoCapture(1) | |
time.sleep(2.0) | |
(W, H) = (None, None) | |
# MobileNetSSD Settings | |
confidenceDef = 0.5 | |
thresholdDef = 0.3 | |
prototxtFile = "MobileNetSSD_deploy.prototxt.txt" | |
modelFile = "MobileNetSSD_deploy.caffemodel" | |
net = (None) | |
classesMobileNetSSD = (None) | |
colorsMobileNetSSD = (None) | |
i = 0 | |
detectionEnabled = False | |
while True: | |
i = i + 1 | |
start_time = time.time() | |
ret, frameOrig = video_capture.read() | |
frame = cv2.resize(frameOrig, frameSize) | |
if(detectionEnabled): | |
analyzeFrame(frame) | |
if (time.time() - start_time ) > 0: | |
fpsInfo = "FPS: " + str(1.0 / (time.time() - start_time)) # FPS = 1 / time to process loop | |
font = cv2.FONT_HERSHEY_DUPLEX | |
cv2.putText(frame, fpsInfo, (10, 20), font, 0.4, (255, 255, 255), 1) | |
cv2.imshow('@elbruno - MobileNetSSD Object Detection', frame) | |
# key controller | |
key = cv2.waitKey(1) & 0xFF | |
if key == ord("d"): | |
if (detectionEnabled == True): | |
detectionEnabled = False | |
else: | |
detectionEnabled = True | |
if key == ord("q"): | |
break | |
video_capture.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment