向列表中的一个键添加两个值,并将列表添加到字典

2024-04-19 10:27:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图得到的中心坐标的边界框时,检测和存储在一个列表中为每个FPS。然后,对于每个检测到的列表中心坐标,我想将其存储在字典中。你知道吗

列表的输出应该是 中心坐标=[“点1”:[x1,y1],“点2”:[x2,y2]。。。。。。。。]你知道吗

从这个列表中,我想把它添加到一个字典中,比如 方框\中心={“点1”:[x1,y1],“点2”:{x2,y2],…..}

import cv2
from darkflow.net.build import TFNet
import numpy as np
import time
from collections import defaultdict
options = {
    'model': 'cfg/yolov2.cfg',
    'load': 'bin/yolov2.weights',
    'threshold': 0.8,
    'gpu': 0.8
}

tfnet = TFNet(options)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]

capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
box_center = defaultdict(list) #to store the list of center coordinates
center_coord = [] #to store the center coordinate at each FPS

while True:
    stime = time.time()
    ret, frame = capture.read()
    if ret:
        results = tfnet.return_predict(frame)
        for color, result in zip(colors, results):
            tl = (result['topleft']['x'], result['topleft']['y'])
            br = (result['bottomright']['x'], result['bottomright']['y'])
            center_x = ((tl[0] + br[0])/2)  #Here it gets the midpoint of two X coordinates
            center_y = ((tl[1] + br[1])/2)  #Here it gets the midpoint of two Y coordinates
            center_coord.append([center_x, center_y]) #Here i append the X and Y coordinate in a list

            label = result['label']
            confidence = result['confidence']
            text = '{}: {:.0f}%'.format(label, confidence * 100)
            frame = cv2.rectangle(frame, tl, br, color, 5)
            frame = cv2.putText(frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
        cv2.imshow('frame', frame)
        print('FPS {:.1f}'.format(1 / (time.time() - stime)))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture.release()
cv2.destroyAllWindows()

Tags: theinbrimport列表timeresult中心