作者只提供了单路摄像头的解决方法,我要实现多路摄像头
放两张效果图,第一张是我弄了6个不同的视频,第二张由于缺乏测试摄像头,于是我就把一个摄像头取6次
我做了以下尝试:
1.把多路信号放到一个列表,然后循环取每一路信号,放到一个batch里,送给模型识别,当然,这里没有封装,你也可以把地址放到opt.py里
url_dir = ['1', '2',‘3’,‘4’,'5','6']
lushu = len(url_dir) for
n,url in enumerate(url_dir):
locals()['stream_' + str(n)] = cv2.VideoCapture(url)
2.中间直接利用作者的DetectionLoader得到数据塞进DetectionProcessor,发现只能识别两路信号(第一路和最后一路)。这个让我觉得很奇怪,于是打印每一个步骤的列表信息,发现在DetectionLoader结束的那个for循环结束,都能把多路信号的识别结果塞进self.Q,但是一结束循环,Q里就只剩一路信号了,而且后面的每一步里,都只有这一路信号,更奇怪的是,即使只有一路信号,每次识别的结果却是两路信号。最终的解决方案是把DetectionLoader和DetectionProcessor内容合并,这样就可以看到Q里有多路信号了。
for k in range(len(orig_img)):
boxes_k = boxes[dets[:,0]==k]
if isinstance(boxes_k, int) or boxes_k.shape[0] == 0:
if self.Q.full():
time.sleep(2)
self.Q.put((orig_img[k], im_name[k],
None, None, None, None, None))
continue
inps = torch.zeros(boxes_k.size(0), 3, opt.inputResH,
opt.inputResW)
pt1 = torch.zeros(boxes_k.size(0), 2)
pt2 = torch.zeros(boxes_k.size(0), 2)
if self.Q.full():
time.sleep(2)
inp = im_to_torch(cv2.cvtColor(orig_img[k], cv2.COLOR_BGR2RGB))
inps, pt1, pt2 = crop_from_dets(inp, boxes_k, inps, pt1, pt2)
self.Q.put((inps, orig_img[k], im_name[k], boxes_k,
scores[dets[:,0]==k], pt1, pt2))
3.考虑到GPU的识别速度,我这边用的是P4000显卡,性能介于1060和1070之间,所以只能多帧取一帧来解决识别速度问题。
if num_frames % 25 == 0:
(grabbed, frame) = locals()['stream_' + str(n)].read()
4.最后每一路得出的结果按名字分成各路信号显示出来
pic_name = im_name.split('_')[0]
if boxes is None:
if opt.save_img or opt.save_video or opt.vis:
img = orig_img
if opt.vis:
# print('none')
# print('im_name='+str(im_name))
cv2.namedWindow('AlphaPose Demo_{}'.format(pic_name),
cv2.WINDOW_NORMAL)
cv2.imshow("AlphaPose Demo_{}".format(pic_name), img)
cv2.waitKey(30)
5.webcam_demo.py的代码只要把49/50行改写成一行代码
det_processor = DetectionLoader(data_loader, batchSize=args.detbatch).start()
以及79行的读取顺序改成跟你匹配的
(orig_img, im_name, boxes, scores, inps, pt1, pt2) = det_processor.read()
以下是完整的dataloader_webcam.py的代码(import就不加上了,都没改动)
class WebcamLoader:
def __init__(self, webcam, batchSize=1, queueSize=0):
# initialize the file video stream along with the boolean
# used to indicate if the thread should be stopped or not
# self.stream = cv2.VideoCapture(webcam)
# # self.stream.set(cv2.CAP_PROP_FPS,10)
# assert self.stream.isOpened(), 'Cannot capture source'
self.stopped = False
# initialize the queue used to store frames read from