Upload New File

parent 7c679e08
import cv2
# Load the pre-trained model
net = cv2.dnn.readNetFromCaffe("mobilenet_iter_73000.caffemodel")
# Load the video
cap = cv2.VideoCapture(0)
while True:
# Read the frame
_, frame = cap.read()
# Get the dimensions of the frame
height, width = frame.shape[:2]
# Create a blob from the frame
blob = cv2.dnn.blobFromImage(frame, 0.007843, (width, height), (127.5, 127.5, 127.5), False)
# Pass the blob through the model
net.setInput(blob)
detections = net.forward()
# Loop over the detections
for i in range(detections.shape[2]):
# Get the confidence of the detection
confidence = detections[0, 0, i, 2]
# Filter out weak detections
if confidence > 0.5:
# Get the class label of the detection
class_id = int(detections[0, 0, i, 1])
# Check if the class label is a mobile phone
if class_id == 7:
# Get the bounding box of the detection
x1 = int(detections[0, 0, i, 3] * width)
y1 = int(detections[0, 0, i, 4] * height)
x2 = int(detections[0, 0, i, 5] * width)
y2 = int(detections[0, 0, i, 6] * height)
# Draw a rectangle around the detection
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
# Show the frame
cv2.imshow("Mobile Phone Detection", frame)
# Exit if the user presses 'q'
if cv2.waitKey(1) == ord('q'):
break
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment