Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

improved code structure #3

Merged
merged 4 commits into from
Aug 5, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 62 additions & 34 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,96 @@

import cv2
import os
from os import path
import time
from datetime import datetime

# Opencv DNN
net = cv2.dnn.readNet("dnn_model/yolov4-tiny.weights", "dnn_model/yolov4-tiny.cfg")
model = cv2.dnn_DetectionModel(net)
model.setInputParams(size=(320, 320), scale=1/255)
model.setInputParams(size=(320, 320), scale=1 / 255)

# Load Class list
classes = []

with open("dnn_model/classes.txt", "r") as f:
for class_name in f.readlines():
class_name = class_name.strip()
classes.append(class_name)

#print("Objects list")
#print("classes")

# Initialize camera
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

# Storing current time in variable
start_time = time.time()

# Configurations
thres = 0.25 # confidence threshold
timeInterval = 5 # timeinterval for capturing
capture = True # capture if True
folder = "images/" # path to store images
# Configurations with their default values set.
thres = os.getenv("THRES", 0.25) # confidence threshold
timeInterval = os.getenv("TIMEINT", 5) # time interval for capturing
capture = os.getenv("CAPTURE", True) # capture if True

# Path to store images
folder = "images/"

if not os.path.exists(folder):
os.mkdir(folder)
try:
if not os.path.exists(folder):
os.mkdir(folder)
except PermissionError as pe:
print(pe)
dpshekhawat marked this conversation as resolved.
Show resolved Hide resolved

# Font/Box style used for labelling object detected
font = cv2.FONT_HERSHEY_PLAIN
font_scale = 1
thick = 2

while True:
if cap.isOpened():
# Get Frames
ret, frame = cap.read()
if ret:
# Object Detection
(class_ids, scores, bboxes) = model.detect(frame, confThreshold = thres, nmsThreshold=.4)
for class_id, score, bbox in zip(class_ids, scores, bboxes):
(x, y, w, h) = bbox
class_name = classes[class_id]

cv2.putText(frame, class_name.capitalize(), (x,y - 10), font, font_scale, (0, 200, 50), thick)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 200, 50), thick)
# Uncomment below lines to see real time feed of object detection
#cv2.imshow("ObjectDetection", frame)
#cv2.waitKey(1)
if int(round(time.time()-start_time, 2)) >= timeInterval:
filename = "{}.png".format(datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))
cv2.imwrite(os.path.join(folder,filename), frame)
start_time = time.time()
if capture != True:
break
# cap.isOpened() to check cap object has started capturing the frame.
while cap.isOpened() and capture:
# Get Frames
ret, frame = cap.read()

if ret:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read method returns True if object is read correctly (reference). We only attempt to detect object if we fail.

# Object Detection
(class_ids, scores, bboxes) = model.detect(
dpshekhawat marked this conversation as resolved.
Show resolved Hide resolved
frame, confThreshold=thres, nmsThreshold=0.4
)
for class_id, score, bbox in zip(class_ids, scores, bboxes):
(x, y, w, h) = bbox
class_name = classes[class_id]

cv2.putText(
frame,
class_name.capitalize(),
(x, y - 10),
font,
font_scale,
(0, 200, 50),
thick,
)

cv2.rectangle(
frame,
(x, y),
(x + w, y + h),
(0, 200, 50),
thick,
)
"""
Uncomment the below lines to see real-time
feed of object detection.
"""
#cv2.imshow("ObjectDetection", frame)
dpshekhawat marked this conversation as resolved.
Show resolved Hide resolved
#cv2.waitKey(1)

if int(round(time.time() - start_time, 2)) >= timeInterval:
filename = "{}.jpeg".format(datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))
dpshekhawat marked this conversation as resolved.
Show resolved Hide resolved
cv2.imwrite(os.path.join(folder, filename), frame)

# Resetting variable to current time
start_time = time.time()

else:
break

cap.release()
cv2.destroyAllWindows()