-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetc.py
42 lines (29 loc) · 1.47 KB
/
detc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
from IPython.display import Image, display
def detect_and_draw_box(filen, model="yolov3-tiny", confidence=0.5):
"""Detects common objects on an image and creates a new image with bounding boxes.
Args:
filename (str): Filename of the image.
model (str): Either "yolov3" or "yolov3-tiny". Defaults to "yolov3-tiny".
confidence (float, optional): Desired confidence level. Defaults to 0.5.
"""
# Images are stored under the images/ directory
img_filepath = f'images/{filen}'
# Read the image into a numpy array
img = cv2.imread(img_filepath)
# Perform the object detection
bbox, label, conf = cv.detect_common_objects(img, confidence=confidence, model=model)
# Print current image's filename
print(f"========================\nImage processed: {filen}\n")
# Create a new image that includes the bounding boxes
output_image = draw_bbox(img, bbox, label, conf)
# Save the image in the directory images_with_boxes
cv2.imwrite(f'static/images_with_boxes/{filen}', output_image)
# Display the image with bounding boxes
display(Image(f'static/images_with_boxes{filen}'))
# Print detected objects with confidence level
for l, c in zip(label, conf):
print(f"Detected object: {l} with confidence level of {c}\n")
return(f"Detected object: {l} with confidence level of {c}\n")