简体   繁体   English

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

[英]Adding two values to one key in lists and add the list to dictionary

I am trying get the center coordinate of the bounding box when detected and store it in a list for each FPS.我正在尝试获取检测到的边界框的中心坐标,并将其存储在每个FPS的列表中。 Then for each detected center coordinate of the list, i want to store it in a dictionary.然后对于列表中每个检测到的中心坐标,我想将其存储在字典中。

The output of list should be like center_coord = ["point1" : [x1, y1], "point2" : [x2, y2], ........ ] list的输出应类似于center_coord = [“ point1”:[x1,y1],“ point2”:[x2,y2],........]

From that list, i want to append it into a dictionary like box_center = {"point1" : [x1, y1], "point2" : {x2, y2],.....}从该列表中,我想将其附加到字典中,例如box_center = {“ point1”:[x1,y1],“ point2”:{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()

I am trying get the center coordinate of the bounding box when detected and store it in a list for each FPS.我正在尝试获取检测到的边界框的中心坐标,并将其存储在每个FPS的列表中。 Then for each detected center coordinate of the list, i want to store it in a dictionary.然后对于列表中每个检测到的中心坐标,我想将其存储在字典中。

The output of list should be like center_coord = ["point1" : [x1, y1], "point2" : [x2, y2], ........ ] list的输出应类似于center_coord = [“ point1”:[x1,y1],“ point2”:[x2,y2],........]

From that list, i want to append it into a dictionary like box_center = {"point1" : [x1, y1], "point2" : {x2, y2],.....}从该列表中,我想将其附加到字典中,例如box_center = {“ point1”:[x1,y1],“ point2”:{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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM