Skip to content

Added the OpenCv Model #11675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all 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
82 changes: 82 additions & 0 deletions Object Detection/ObjectDection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import cv2

Check failure on line 1 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

Object Detection/ObjectDection.py:1:1: INP001 File `Object Detection/ObjectDection.py` is part of an implicit namespace package. Add an `__init__.py`.

Check failure on line 1 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

Object Detection/ObjectDection.py:1:1: INP001 File `Object Detection/ObjectDection.py` is part of an implicit namespace package. Add an `__init__.py`.
import matplotlib.pyplot as plt

config_file = "ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
frozen_model = "frozen_inference_graph.pb"
model = cv2.dnn_DetectionModel(frozen_model, config_file)

classLabels = []

Check failure on line 8 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N816)

Object Detection/ObjectDection.py:8:1: N816 Variable `classLabels` in global scope should not be mixedCase

Check failure on line 8 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N816)

Object Detection/ObjectDection.py:8:1: N816 Variable `classLabels` in global scope should not be mixedCase

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: classLabels

file_name = "labels.txt"
with open(file_name, "rt") as fpt:

Check failure on line 10 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP015)

Object Detection/ObjectDection.py:10:6: UP015 Unnecessary open mode parameters

Check failure on line 10 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP015)

Object Detection/ObjectDection.py:10:6: UP015 Unnecessary open mode parameters
classLabels = fpt.read().rstrip("\n").split("\n")

Check failure on line 11 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N816)

Object Detection/ObjectDection.py:11:5: N816 Variable `classLabels` in global scope should not be mixedCase

Check failure on line 11 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N816)

Object Detection/ObjectDection.py:11:5: N816 Variable `classLabels` in global scope should not be mixedCase

print(classLabels)
print(len(classLabels))

model.setInputSize(320, 320)
model.setInputScale(1.0 / 127.5)
model.setInputMean((127.5, 127.5, 127.5))
model.setInputSwapRB(True)

img = cv2.imread("boy.jpg")
plt.imshow(img)

ClassIndex, confidence, bbox = model.detect(img, confThreshold=0.5)

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: ClassIndex

print(ClassIndex)

font_scale = 3
font = cv2.FONT_HERSHEY_PLAIN
for ClassInd, conf, boxes in zip(ClassIndex.flatten(), confidence.flatten(), bbox):

Check failure on line 29 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (B007)

Object Detection/ObjectDection.py:29:15: B007 Loop control variable `conf` not used within loop body

Check failure on line 29 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (B007)

Object Detection/ObjectDection.py:29:15: B007 Loop control variable `conf` not used within loop body
cv2.rectangle(img, boxes, (255, 0, 0), 2)
cv2.putText(
img,
classLabels[ClassInd - 1],
(boxes[0] + 10, boxes[1] + 40),
font,
fontScale=font_scale,
color=(0, 255, 0),
thickness=3,
)

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()

# Webcam Object Detection
cap = cv2.VideoCapture("1")
if not cap.isOpened():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
raise IOError("Cannot open video")

Check failure on line 49 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP024)

Object Detection/ObjectDection.py:49:11: UP024 Replace aliased errors with `OSError`

Check failure on line 49 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP024)

Object Detection/ObjectDection.py:49:11: UP024 Replace aliased errors with `OSError`

font_scale = 3
font = cv2.FONT_HERSHEY_PLAIN

while True:
ret, frame = cap.read()

ClassIndex, confidence, bbox = model.detect(frame, confThreshold=0.55)

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: ClassIndex

print(ClassIndex)

if len(ClassIndex) != 0:
for ClassInd, conf, boxes in zip(

Check failure on line 61 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (B007)

Object Detection/ObjectDection.py:61:23: B007 Loop control variable `conf` not used within loop body

Check failure on line 61 in Object Detection/ObjectDection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (B007)

Object Detection/ObjectDection.py:61:23: B007 Loop control variable `conf` not used within loop body
ClassIndex.flatten(), confidence.flatten(), bbox
):
if ClassInd <= 80:
cv2.rectangle(frame, boxes, (255, 0, 0), 2)
cv2.putText(
frame,
classLabels[ClassInd - 1],
(boxes[0] + 10, boxes[1] + 40),
font,
fontScale=font_scale,
color=(0, 255, 0),
thickness=3,
)

cv2.imshow("Object Detection Tutorial", frame)

if cv2.waitKey(2) & 0xFF == ord("q"):
break

cap.release()
cv2.destroyAllWindows()
Loading