Spaces:
No application file
No application file
Commit
•
ec02f42
1
Parent(s):
c17ef57
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yolov5
|
2 |
+
|
3 |
+
# load model
|
4 |
+
model = yolov5.load('keremberke/yolov5m-garbage')
|
5 |
+
|
6 |
+
# set model parameters
|
7 |
+
model.conf = 0.25 # NMS confidence threshold
|
8 |
+
model.iou = 0.45 # NMS IoU threshold
|
9 |
+
model.agnostic = False # NMS class-agnostic
|
10 |
+
model.multi_label = False # NMS multiple labels per box
|
11 |
+
model.max_det = 1000 # maximum number of detections per image
|
12 |
+
|
13 |
+
# set image
|
14 |
+
img = 'sample.jpg'
|
15 |
+
|
16 |
+
# perform inference
|
17 |
+
results = model(img, size=640)
|
18 |
+
|
19 |
+
# inference with test time augmentation
|
20 |
+
results = model(img, augment=True)
|
21 |
+
|
22 |
+
# parse results
|
23 |
+
predictions = results.pred[0]
|
24 |
+
boxes = predictions[:, :4] # x1, y1, x2, y2
|
25 |
+
scores = predictions[:, 4]
|
26 |
+
categories = predictions[:, 5]
|
27 |
+
|
28 |
+
# show detection bounding boxes on image
|
29 |
+
results.show()
|
30 |
+
|
31 |
+
print(results) # results output image 1/1: 720x1280 2 biodegradables, 1 paper
|
32 |
+
|
33 |
+
sample = str(results).split()
|
34 |
+
|
35 |
+
|
36 |
+
# print(type(results))
|
37 |
+
|
38 |
+
# regex to match the particular things
|
39 |
+
|
40 |
+
ct =0
|
41 |
+
for words in sample:
|
42 |
+
if(words=='paper' or words =='plastic' or words =='rubber'):
|
43 |
+
ct+=1
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
if(ct >5):
|
48 |
+
print("Image contains garbage")
|
49 |
+
else:
|
50 |
+
print("Image does not contain garbage")
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
# save results into "results/" folder
|
55 |
+
# results.save(save_dir='results/')
|