>># 将DOM对象doc写⼊⽂件xmlname = Cxml.path[:-3]+"xml"
print("save to :" ,xmlname)
f = open(xmlname, 'w')
# f.prettyxml(indent = '\t', newl = '\n', encoding = 'utf-8'))
doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
f.clo()
def readimg(frame,Cxml):
frame_id = 0
frame_id += 1
# frame = size(frame, (640, 480))
blob = cv2.dnn.blobFromImage(frame, 1 / 255, (416, 416), swapRB=True)
net.tInput(blob)
outs = net.forward(output_layers)
confidences = []
class_ids = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_thresh:
height, width, channels = frame.shape
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
confidences.append(float(confidence))
class_ids.append(class_id)
boxes.append([x, y, w, h])
indexes = cv2.dnn.NMSBoxes(boxes, confidences, conf_thresh, 0.4)
mdict = []
for i in range(len(boxes)):
# If the box remained after NMS.
if i in indexes:
x, y, w, h = boxes[i]
label = str(class[class_ids[i]])
mlist = [label,x,y,x+w,y+h]
mdict.append(mlist)
confidence = confidences[i]
color = colors[class_ids[i]]
# Draw the box.
cv2.putText(frame, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
Cxml.tlabel(mdict)
creatxml(Cxml)
elapd_time = time.time() - time_start
fps = frame_id / elapd_time
cv2.putText(frame, "FPS: " + str(round(fps, 2)), (8, 30), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 2)
# cv2.imshow("Camera", frame)
最好的在线翻译
# cv2.waitKey(2)
if __name__ == '__main__':
JPEGpath = 'image'
# 测试图⽚,
path = os.getcwd()
path = os.path.join(path,JPEGpath)
net = adNet("./model/yolov4-tiny.weights", "./model/yolov4-tiny.cfg")
layer_names = LayerNames()
output_layers = [layer_names[i[0] - 1] for i UnconnectedOutLayers()]
with open("./model/coco.names", "r") as f:
class = [line.strip() for line adlines()]
colors = np.random.uniform(0, 255, size=(len(class), 3))
for i in os.listdir(path): dswith('jpg') dswith('png'): zpath = os.path.join(path, i) img = cv2.imread(zpath) # newpath = place('test', 'voc2007de') Cxml = xmlvalue(i,JPEGpath,zpath,img) readimg(img,Cxml) # rtsp = "rtsp://admin:cqi