Spaces:
No application file
No application file
File size: 1,191 Bytes
ec02f42 |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import yolov5
# load model
model = yolov5.load('keremberke/yolov5m-garbage')
# set model parameters
model.conf = 0.25 # NMS confidence threshold
model.iou = 0.45 # NMS IoU threshold
model.agnostic = False # NMS class-agnostic
model.multi_label = False # NMS multiple labels per box
model.max_det = 1000 # maximum number of detections per image
# set image
img = 'sample.jpg'
# perform inference
results = model(img, size=640)
# inference with test time augmentation
results = model(img, augment=True)
# parse results
predictions = results.pred[0]
boxes = predictions[:, :4] # x1, y1, x2, y2
scores = predictions[:, 4]
categories = predictions[:, 5]
# show detection bounding boxes on image
results.show()
print(results) # results output image 1/1: 720x1280 2 biodegradables, 1 paper
sample = str(results).split()
# print(type(results))
# regex to match the particular things
ct =0
for words in sample:
if(words=='paper' or words =='plastic' or words =='rubber'):
ct+=1
if(ct >5):
print("Image contains garbage")
else:
print("Image does not contain garbage")
# save results into "results/" folder
# results.save(save_dir='results/')
|