Update project with minor fixes and improvements

This commit is contained in:
Diego Inácio
2022-06-24 14:53:57 -03:00
parent 7cb96779e4
commit 478e3d8837
5 changed files with 110 additions and 91 deletions

View File

@ -1,3 +1,4 @@
import os
import time
import cv2
import numpy as np
@ -5,27 +6,34 @@ import numpy as np
class ObjectDetection:
def __init__(self):
PROJECT_PATH = os.path.abspath(os.getcwd())
MODELS_PATH = os.path.join(PROJECT_PATH, "models")
self.MODEL = cv2.dnn.readNet(
'models/yolov3.weights',
'models/yolov3.cfg'
os.path.join(MODELS_PATH, "yolov3.weights"),
os.path.join(MODELS_PATH, "yolov3.cfg")
)
self.CLASSES = []
with open("models/coco.names", "r") as f:
with open(os.path.join(MODELS_PATH, "coco.names"), "r") as f:
self.CLASSES = [line.strip() for line in f.readlines()]
self.OUTPUT_LAYERS = [self.MODEL.getLayerNames()[i[0] - 1] for i in self.MODEL.getUnconnectedOutLayers()]
self.OUTPUT_LAYERS = [
self.MODEL.getLayerNames()[i - 1] for i in self.MODEL.getUnconnectedOutLayers()
]
self.COLORS = np.random.uniform(0, 255, size=(len(self.CLASSES), 3))
self.COLORS /= (np.sum(self.COLORS**2, axis=1)**0.5/255)[np.newaxis].T
def detectObj(self, snap):
height, width, channels = snap.shape
blob = cv2.dnn.blobFromImage(snap, 1/255, (416, 416), swapRB=True, crop=False)
blob = cv2.dnn.blobFromImage(
snap, 1/255, (416, 416), swapRB=True, crop=False
)
self.MODEL.setInput(blob)
outs = self.MODEL.forward(self.OUTPUT_LAYERS)
# Showing informations on the screen
# ! Showing informations on the screen
class_ids = []
confidences = []
boxes = []
@ -35,13 +43,13 @@ class ObjectDetection:
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Object detected
# * Object detected
center_x = int(detection[0]*width)
center_y = int(detection[1]*height)
w = int(detection[2]*width)
h = int(detection[3]*height)
# Rectangle coordinates
# * Rectangle coordinates
x = int(center_x - w/2)
y = int(center_y - h/2)
@ -97,7 +105,7 @@ class VideoStreaming(object):
@detect.setter
def detect(self, value):
self._detect = bool(value)
@property
def exposure(self):
return self._exposure
@ -106,7 +114,7 @@ class VideoStreaming(object):
def exposure(self, value):
self._exposure = value
self.VIDEO.set(cv2.CAP_PROP_EXPOSURE, self._exposure)
@property
def contrast(self):
return self._contrast
@ -121,7 +129,7 @@ class VideoStreaming(object):
ret, snap = self.VIDEO.read()
if self.flipH:
snap = cv2.flip(snap, 1)
if ret == True:
if self._preview:
# snap = cv2.resize(snap, (0, 0), fx=0.5, fy=0.5)
@ -133,16 +141,17 @@ class VideoStreaming(object):
int(self.VIDEO.get(cv2.CAP_PROP_FRAME_HEIGHT)),
int(self.VIDEO.get(cv2.CAP_PROP_FRAME_WIDTH))
), np.uint8)
label = 'camera disabled'
label = "camera disabled"
H, W = snap.shape
font = cv2.FONT_HERSHEY_PLAIN
color = (255,255,255)
cv2.putText(snap, label, (W//2 - 100, H//2), font, 2, color, 2)
frame = cv2.imencode('.jpg', snap)[1].tobytes()
color = (255, 255, 255)
cv2.putText(snap, label, (W//2 - 100, H//2),
font, 2, color, 2)
frame = cv2.imencode(".jpg", snap)[1].tobytes()
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
time.sleep(0.01)
else:
break
print('off')
print("off")