Я столкнулся с двумя проблемами из-за того, что обнаружение YOLO не отображается в кадре -
Ошибка - ValueError: невозможно преобразовать float NaN в целое число
Ошибка - w = int (обнаружение [2] * ширина) OverflowError: невозможно преобразовать бесконечность с плавающей запятой в целое число
My Цель состоит в том, чтобы получить доступ к веб-камере на машине клиента, обработать кадр и обнаружить объект в соответствии с предварительно обученной моделью Yolo.
Точная ошибка на терминале Ma c - center_x = int (обнаружение [0] * ширина) ValueError: невозможно преобразовать число с плавающей запятой NaN в целое число
Совместное использование кода для используемых важных файлов - app. запустить, обнаружить. html, detect_processing.py, training_model.py
run.py
@socketio.on('image')
def image(data_image):
# decode and convert into image
b = io.BytesIO(base64.b64decode(data_image))
pimg = Image.open(b)
pimg = numpy.array(pimg)
#print(pimg)
# Process the image frame
#frame = imutils.resize(pimg, width=500, height=375)
frameb = cv2.cvtColor(pimg, cv2.COLOR_RGB2BGR)
frameb = cv2.flip(frameb, 1)
frameb = detect_object(frameb)
imgencode = cv2.imencode('.jpg', frameb)[1]
# base64 encode
stringData = base64.b64encode(imgencode).decode('utf-8')
b64_src = 'data:image/jpg;base64,'
stringData = b64_src + stringData
# emit the frame back
emit('response_back', stringData)
# main driver function
if __name__ == '__main__':
socketio.run(app,debug=True)
detect_processing.py
import cv2
import numpy as np
from trained_model import *
def detect_object(frame):
# DO WHAT YOU WANT WITH TENSORFLOW / KERAS AND OPENCV
# Implement YOLO object detection model
# keep outside while loop so that we define it once and not repeat defining it in the loop
font=cv2.FONT_HERSHEY_SIMPLEX
height, width, channels = frame.shape
print(width, height)
#Detecing images using cv2 neural network (We can change the processing size of input frame for improving speed on cpu)
blob = cv2.dnn.blobFromImage(frame,0.00392,(320,320),(0,0,0),True,crop = False)
#passing blob through the network to detect and prediction
#Outs is an array that conains all the informations about objects detected,
#their position and the confidence about the detection.
net.setInput(blob)
outs = net.forward(output_layer)
# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
text = "{:.2f}%".format(confidence * 100)
#When we perform the detection, it happens that we have more boxes for the same object, so we should use another function to remove this “noise”.
#It’s called Non maximum suppresion.
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
#Box: contain the coordinates of the rectangle sorrounding the object detected.
#Label: it’s the name of the object detected
#Confidence: the confidence about the detection from 0 to 1
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
label_plus = label + " - " + text
#label_plus = label_plus[::-1]
#frame=cv2.flip(frame,1)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0,0,255), 3)
cv2.putText(frame, label_plus, (x, y - 20), font, 0.6, (0,255,255), 1)
return frame
trained_model.py
import cv2
#we have choosed the yolo version which works best on image of size 416*416
#we can use yolov3 - tiny version as it has been optimized for CPU but detection won't be that great
path1 = "/Users/sonijha/Downloads/Yolo_ObjectDetection/yolov3_416.weights"
path2 = "/Users/sonijha/Downloads/Yolo_ObjectDetection/yolov3.cfg"
net = cv2.dnn.readNet(path1, path2)
#Reading from coco.names for 80 objects which are pre-trained by yolo
#with statement in Python is used in exception handling to make the code cleaner and much more readable.
classes = []
with open ("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
print(classes[73])
layer_names = net.getLayerNames()
#getLayerNames(): Get the name of all layers of the network.
#getUnconnectedOutLayers(): Get the index of the output layers.
output_layer = [layer_names[i[0]-1] for i in net.getUnconnectedOutLayers()]