Hello,
I would like to make an MXB headlamp for my motorbike with the help of Python and OpenCV, so practically to detect persons, cars, motorcycles, busses and so on…on the road, and then turn of there the light stripe in that region so that it gets blended out.
With the help of youtube videos i have managed to get this far:
But i don’t know how to do the following:
in the segments from 1 to 12, i have to detect the persons, cars, motorcycles, and then i should be able to define an action for that region.
Example1: the person is detected in the region MXB1:MXB9(picture above) → all these stripes should go off, all others should remain on.
Example2: a car is being detected in the region of MXB2, then only the mxb2 should go off, and all others should remain on, so that you can define for which classIDS the action should be performed in an designated area.
could you help me with a hint how i can perform this action ?
I don’t know if you have understood what i want to achieve, but i hope so.
Thank you
Flo
my code is here:
import cv2
import numpy as np
from cv2 import dnn_DetectionModel
import time
#import tensorflow as tf
#import argparse
#import sys
thres = 0.45 # Threshold to detect object
cap = cv2.VideoCapture(0)
cap.set(3, 1280)
cap.set(4, 720)
cap.set(10, 70)
# Set up camera constants
IM_WIDTH = 1280
IM_HEIGHT = 720
classNames = []
classFile = 'coco.names'
with open(classFile,'rt') as f:
classNames = f.read().rstrip('\n').split('\n')
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'
net: dnn_DetectionModel = cv2.dnn_DetectionModel(weightsPath, configPath)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
# Define MXB1 box coordinates (top left and bottom right)
TL_mx1 = (int(IM_WIDTH * 0.40), int(IM_HEIGHT * 0.01))
BR_mx1 = (int(IM_WIDTH * 0.44), int(IM_HEIGHT * 0.7))
# Define MXB2 box coordinates (top left and bottom right)
TL_mx2 = (int(IM_WIDTH * 0.44), int(IM_HEIGHT * 0.01))
BR_mx2 = (int(IM_WIDTH * 0.48), int(IM_HEIGHT * 0.7))
# Define MXB3 box coordinates (top left and bottom right)
TL_mx3 = (int(IM_WIDTH * 0.48), int(IM_HEIGHT * 0.01))
BR_mx3 = (int(IM_WIDTH * 0.52), int(IM_HEIGHT * 0.7))
# Define MXB4 box coordinates (top left and bottom right)
TL_mx4 = (int(IM_WIDTH * 0.52), int(IM_HEIGHT * 0.01))
BR_mx4 = (int(IM_WIDTH * 0.56), int(IM_HEIGHT * 0.7))
# Define MXB5 box coordinates (top left and bottom right)
TL_mx5 = (int(IM_WIDTH * 0.56), int(IM_HEIGHT * 0.01))
BR_mx5 = (int(IM_WIDTH * 0.60), int(IM_HEIGHT * 0.7))
# Define MXB6 box coordinates (top left and bottom right)
TL_mx6 = (int(IM_WIDTH * 0.60), int(IM_HEIGHT * 0.01))
BR_mx6 = (int(IM_WIDTH * 0.64), int(IM_HEIGHT * 0.7))
# Define MXB7 box coordinates (top left and bottom right)
TL_mx7 = (int(IM_WIDTH * 0.64), int(IM_HEIGHT * 0.01))
BR_mx7 = (int(IM_WIDTH * 0.68), int(IM_HEIGHT * 0.7))
# Define MXB8 box coordinates (top left and bottom right)
TL_mx8 = (int(IM_WIDTH * 0.68), int(IM_HEIGHT * 0.01))
BR_mx8 = (int(IM_WIDTH * 0.72), int(IM_HEIGHT * 0.7))
# Define MXB9 box coordinates (top left and bottom right)
TL_mx9 = (int(IM_WIDTH * 0.72), int(IM_HEIGHT * 0.01))
BR_mx9 = (int(IM_WIDTH * 0.76), int(IM_HEIGHT * 0.7))
# Define MXB10 box coordinates (top left and bottom right)
TL_mx10 = (int(IM_WIDTH * 0.76), int(IM_HEIGHT * 0.01))
BR_mx10 = (int(IM_WIDTH * 0.80), int(IM_HEIGHT * 0.7))
# Define MXB11 box coordinates (top left and bottom right)
TL_mx11 = (int(IM_WIDTH * 0.80), int(IM_HEIGHT * 0.01))
BR_mx11 = (int(IM_WIDTH * 0.84), int(IM_HEIGHT * 0.7))
# Define MXB12 box coordinates (top left and bottom right)
TL_mx12 = (int(IM_WIDTH * 0.84), int(IM_HEIGHT * 0.01))
BR_mx12 = (int(IM_WIDTH * 0.95), int(IM_HEIGHT * 0.7))
# Initialize control variables used for person detector
detected_inside = False
detected_outside = False
inside_counter = 0
outside_counter = 0
pause = 0
pause_counter = 0
font = cv2.FONT_HERSHEY_SIMPLEX
pTime = 0
while True:
success, img = cap.read()
confs: object
classIds, confs, bbox = net.detect(img, confThreshold=0.5)
bbox = list(bbox)
confs = list(np.array(confs).reshape(1,-1)[0])
confs = list(map(float,confs))
#print(type(confs[0]))
print(confs)
indices = cv2.dnn.NMSBoxes(bbox, confs, thres, nms_threshold=0.2)
print(indices, bbox)
for i in indices:
#i = i[0]
box = bbox[i]
x,y,w,h = box[0],box[1],box[2],box[3]
cv2.rectangle(img, TL_mx1, BR_mx1, (255, 20, 20), 1)
cv2.putText(img, "1", (TL_mx1[0] + 10, TL_mx1[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx2, BR_mx2, (255, 20, 20), 1)
cv2.putText(img, "2", (TL_mx2[0] + 10, TL_mx2[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx3, BR_mx3, (255, 20, 20), 1)
cv2.putText(img, "3", (TL_mx3[0] + 10, TL_mx3[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx4, BR_mx4, (255, 20, 20), 1)
cv2.putText(img, "4", (TL_mx4[0] + 10, TL_mx4[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx5, BR_mx5, (255, 20, 20), 1)
cv2.putText(img, "5", (TL_mx5[0] + 10, TL_mx5[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx6, BR_mx6, (255, 20, 20), 1)
cv2.putText(img, "6", (TL_mx6[0] + 10, TL_mx6[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx7, BR_mx7, (255, 20, 20), 1)
cv2.putText(img, "7", (TL_mx7[0] + 10, TL_mx7[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx8, BR_mx8, (255, 20, 20), 1)
cv2.putText(img, "8", (TL_mx8[0] + 10, TL_mx8[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx9, BR_mx9, (255, 20, 20), 1)
cv2.putText(img, "9", (TL_mx9[0] + 10, TL_mx9[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx10, BR_mx10, (255, 20, 20), 1)
cv2.putText(img, "10", (TL_mx10[0] + 10, TL_mx10[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx11, BR_mx11, (255, 20, 20), 1)
cv2.putText(img, "11", (TL_mx11[0] + 10, TL_mx11[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, TL_mx12, BR_mx12, (255, 20, 20), 1)
cv2.putText(img, "12", (TL_mx12[0] + 10, TL_mx12[1] + 30), font, 1, (255, 20, 255), 3,
cv2.LINE_AA)
cv2.rectangle(img, (x,y), (x+w,h+y), color=(0, 255, 0), thickness=2)
cv2.putText(img,f'{classNames[classIds[i]-1].upper()} {int(confs[i]*100)}%', (x,y -10),
cv2.FONT_HERSHEY_COMPLEX, 0.6, (0, 255, 0), 2)
# if len(classIds) != 0:
# for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
# cv2.rectangle(img, box, color=(0, 255, 0), thickness=1),
# cv2.putText(img, classNames[classId-1].upper(), (box[0] + 10, box[1] + 10),
# cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
# cv2.putText(img, str(round(confidence * 100, 2)), (box[0] + 200, box[1] + 30),
# cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
#Here the FPS are generated
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, f'FPS: {int(fps)}', (20,70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0),2)
cv2.imshow("Output", img)
cv2.waitKey(1)