我是OpenCV python以及StackOverflow的初学者,所以请原谅我任何错误。我正在使用OpenCV 3.2和Python 3.6进行图像分类任务。我的任务是对帧中的对象进行分类,并在该帧上给出其类标签。当写回最终输出视频时,放在视频上的文本会重叠。下面是我的代码。
cap = cv2.VideoCapture(r"D:\python\tank_video\All_vehicle.mp4")
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
currentFrame = 0
# videowriter object
codc = int(cap.get(cv2.CAP_PROP_FOURCC))
fps = int(cap.get(cv2.CAP_PROP_FPS))
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('classify_300_200_3.avi', fourcc, 15, (300,200))
"CLASSIFICATION CODE"
predictions = [classes_names[i] for i in clf.predict(test_features)]
arr = np.array([])
j = 0
for prediction in predictions:
pt = (0, 3 * image.shape[0] // 4)
cv2.putText(image, prediction, pt ,cv2.FONT_HERSHEY_SIMPLEX, 2, [0, 255,
0], 2)
np.append(arr,cv2.imwrite(str(j)+'.jpg',image))
out.write(image)
currentFrame += 1
cap.release()
out.release()
cv2.destroyAllWindows()
输出图像已附在此处。this is the screenshot of video output.我想要显示一个类标签。不应显示前一帧的标签,并且不应在前一帧的标签上覆盖当前帧的标签。任何人都可以帮我解决吗?
答案 0 :(得分:1)
如果您只想显示一个类标签,则应更改以下内容:
for prediction in predictions:
pt = (0, 3 * image.shape[0] // 4)
cv2.putText(image, prediction, pt ,cv2.FONT_HERSHEY_SIMPLEX, 2, [0, 255,
0], 2)
np.append(arr,cv2.imwrite(str(j)+'.jpg',image))
因为那段代码会将检测到的所有类写入图像。例如,你可以这样做:
# check if at least one class detected
if len(predictions)>0:
# choose to display only the first class
prediction = predictions[0]
pt = (0, 3 * image.shape[0] // 4)
cv2.putText(image, prediction, pt ,cv2.FONT_HERSHEY_SIMPLEX, 2, [0, 255,
0], 2)
np.append(arr,cv2.imwrite(str(j)+'.jpg',image))