'find the biggest ball - detect object
i am a newby in python and i am researching about detect object. I'm looking for the biggest ball. I have no idea how to make it work. Can anybody give me some pointers? Here is what I have so far. my code: (from: "https://youtu.be/Vg9rrOFmwHo")
import cv2
import serial
from time import sleep
classNames = []
classFile = "/home/pi/Desktop/Object_Detection_Files/coco.names"
with open(classFile,"rt") as f:
classNames = f.read().rstrip("\n").split("\n")
configPath = "/home/pi/Desktop/Object_Detection_Files/ssd_mobilenet.pbtxt"
weightsPath = "/home/pi/Desktop/Object_Detection_Files/frozen_inference_graph.pb"
net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
def getObjects(img, thres, nms, draw=True, objects=[]):
classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
if len(objects) == 0: objects = classNames
objectInfo =[]
if len(classIds) != 0:
for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
className = classNames[classId - 1]
if className in objects:
objectInfo.append([box,className])
if (draw):
cv2.rectangle(img,box,color=(0,255,0),thickness=2)
cv2.putText(img,'sports ball',(box[0]+10,box[1]+30),
cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2)
x= int(box[0]+box[2]/2)
y= int(box[1]+box[3]/2)
cv2.circle(img, (x, y), 5, (0,255,0), 1)
return img,objectInfo
if __name__ == "__main__":
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
while True:
success, img = cap.read()
height, width, _ = img.shape
result, objectInfo = getObjects(img,0.29,0.2, objects=['sports ball'] )
#print(objectInfo)
cv2.imshow("Output",img)
cv2.waitKey(1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
