我训练了用于手模型的深度学习分类,并与opencv的对象跟踪代码一起使用。但是,我在扩张阶段遇到错误。没有说彩色框,掩码阈值全为零数组。我可以存储框架,但在跑步过程中无法显示正确的框架,只能显示黑色背景。
我试图打印一些调试日志。
Date : 7th Jan 2018
Author : xiaochus
Email : xiaochus@live.cn
Affiliation : School of Computer Science and Communication Engineering
- Jiangsu University - China
License : MIT
Status : Under Active Development
Description :
OpenCV 3 & Keras implementation of the vehicle tracking.
"""
import sys
import copy
import argparse
import cv2
import numpy as np
from keras.models import load_model
import os
from utils.entity import Entity
def main(argv):
parser = argparse.ArgumentParser()
# Required arguments.
parser.add_argument(
"--file",
help="Input video file.",
)
# Optional arguments.
parser.add_argument(
"--iou",
default=0.2,
help="threshold for tracking",
)
args = parser.parse_args()
track(args.file, args.iou)
def overlap(box1, box2):
"""
Check the overlap of two boxes
"""
endx = max(box1[0] + box1[2], box2[0] + box2[2])
startx = min(box1[0], box2[0])
width = box1[2] + box2[2] - (endx - startx)
endy = max(box1[1] + box1[3], box2[1] + box2[3])
starty = min(box1[1], box2[1])
height = box1[3] + box2[3] - (endy - starty)
if (width <= 0 or height <= 0):
return 0
else:
Area = width * height
Area1 = box1[2] * box1[3]
Area2 = box2[2] * box2[3]
ratio = Area / (Area1 + Area2 - Area)
return ratio
def track(video, iou):
print(video)
print(os.getcwd())
print(os.path.join(os.getcwd(), video))
camera = cv2.VideoCapture(video)
print(type(camera))
res, frame = camera.read()
y_size = frame.shape[0]
x_size = frame.shape[1]
# Load CNN classification model
model = load_model('brightness3ch.h5')
# Definition of MOG2 Background Subtraction
bs = cv2.createBackgroundSubtractorMOG2(detectShadows=True)
history = 20
frames = 0
counter = 0
track_list = []
cv2.namedWindow("detection", cv2.WINDOW_NORMAL)
while True:
res, frame = camera.read()
y_size = frame.shape[0]
x_size = frame.shape[1]
cv2.imwrite("result.png", frame)
if res is False:
break
# Train the MOG2 with first frames frame
fg_mask = bs.apply(frame)
if frames < history:
frames += 1
continue
# Expansion and denoising the original frame
th = cv2.threshold(fg_mask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
print(th)
th = cv2.erode(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
dilated = cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8, 3)), iterations=2)
image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Check the bouding boxs
for c in contours:
x, y, w, h = cv2.boundingRect(c)
if cv2.contourArea(c) > 3000:
# Extract roi
img = frame[y: y + h, x: x + w, :]
rimg = cv2.resize(img, (64, 64), interpolation=cv2.INTER_CUBIC)
image_data = np.array(rimg, dtype='float32')
image_data /= 255.
roi = np.expand_dims(image_data, axis=0)
flag = model.predict(roi)
print(flag)
if flag[0][0] >= 0 and flag[0][0] <=9:
e = Entity(counter, (x, y, w, h), frame)
# Exclude existing targets in the tracking list
if track_list:
count = 0
num = len(track_list)
for p in track_list:
if overlap((x, y, w, h), p.windows) < iou:
count += 1
if count == num:
track_list.append(e)
else:
track_list.append(e)
counter += 1
# Check and update goals
if track_list:
tlist = copy.copy(track_list)
for e in tlist:
x, y = e.center
if 10 < x < x_size - 10 and 10 < y < y_size - 10:
e.update(frame)
else:
track_list.remove(e)
frames += 1
cv2.imshow("detection", frame)
if cv2.waitKey(110) & 0xff == 27:
break
camera.release()
if __name__ == '__main__':
main(sys.argv)
错误日志:
D:\ PythonSpace \ Hand_Tracking> python track_finger.py --file car.flv 使用TensorFlow后端car.flv D:\ PythonSpace \ Hand_Tracking D:\ PythonSpace \ Hand_Tracking \ car.flv 2019-03-22 11:51:22.024142:我 tensorflow / core / platform / cpu_feature_guard.cc:141]您的CPU支持 TensorFlow二进制文件未编译使用的指令:AVX2 C:\ Python \ Python37 \ lib \ site-packages \ keras \ engine \ saving.py:292: UserWarning:在保存文件中找不到训练配置:模型 未编译。手动编译。 warnings.warn('不接受任何培训 在保存文件中找到配置:'[[0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] ... [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0]]追溯(最近一次通话):文件“ track_finger.py”, 如果name =='main',则输入第157行:文件“ track_finger.py”,输入44 主参数= parser.parse_args()文件“ track_finger.py”,第109行,在 磁道膨胀= cv2.dilate(th, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(8,3)),迭代次数= 2) ValueError:没有足够的值可解压缩(预期3,得到2)