如何使用python和opencv将矩形,圆形和文本绘制到视频文件上?

时间:2020-06-26 15:55:36

标签: python opencv

我有两个文件:一个包含注释和眼动数据的pkl文件,以及一个需要在其中绘制注释和眼动数据的视频文件。注释需要通过矩形和文本绘制,而眼睛跟踪数据需要通过两个圆(每只眼睛一个)绘制。下面的附件是我的脚本,用于将pkl文件绘制到视频文件上。

import cv2
import pickle
import numpy as np
import aframe

def plot_faces(vid_in, annot_in, fsave):
    # open the vid_in file as a VideoCapture object
    cap = cv2.VideoCapture(vid_in)

    # define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter(fsave, fourcc, 30.0, (1280, 554))

    # open the annotation file
    # annot = pickle.load(open(annot_in, 'rb'))
    annot = np.load(annot_in, allow_pickle=True)
    annot1 = annot['Annotation']
    # for key in annot1.keys():
    #     print(key, '-->', annot1[key])

    # font for writing words
    font = cv2.FONT_HERSHEY_SIMPLEX

    framenum = 1
    while cap.isOpened():
        ret, img = cap.read()
        # print(ret)

        if ret == True:
            # check if annotations exist for this frame
            if framenum in annot:
                print(framenum)
                # get all annotations on this frame
                annot_frame = annot1[framenum]

                # iterate through the boxes on this frame (box id's are arbitrary)
                for box_id in annot_frame:
                    print(box_id)
                    # make sure this box wasn't hidden

                    if annot_frame[box_id]['meta']['outside'] != '1':
                        # get box coordinates
                        bbox = [int(float(annot_frame[box_id]['meta']['xtl'])),
                                int(float(annot_frame[box_id]['meta']['ytl'])),
                                int(float(annot_frame[box_id]['meta']['xbr'])),
                                int(float(annot_frame[box_id]['meta']['ybr'])), ]

                        print(bbox)

                        # get character name
                        character = annot_frame[box_id]['character']

                        # TODO: Instead of character names, plot the emotion and intensity
                        # Can modify the colors/font sizes below
                        emotion = annot_frame[box_id]['emotion']
                        if emotion['angry']['present'] == 1:
                            intensity = annot_frame[box_id]['intensity']
                        elif emotion['disgust']['present'] == 1:
                            intensity = annot_frame[box_id]['intensity']
                        elif emotion['fear']['present'] == 1:
                            intensity = annot_frame[box_id]['intensity']
                        elif emotion['happy']['present'] == 1:
                            intensity = annot_frame[box_id]['intensity']
                        elif emotion['sad']['present'] == 1:
                            intensity = annot_frame[box_id]['intensity']
                        elif emotion['surprise']['present'] == 1:
                            intensity = annot_frame[box_id]['intensity']
                        else:
                            break

                        # draw the box and character name on this movie frame
                        # note: the second-to-last parameter is color in the format of (Blue, Green, Red)
                        cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (25, 250, 100), 2)
                        cv2.putText(img, str(character), (bbox[0], bbox[1] + 15), font, 1.0, (0, 0, 100), 2, cv2.LINE_AA)
                        cv2.putText(img, str(emotion), (bbox[0], bbox[1] + 50), font, 1.0, (100, 250, 25), 2, cv2.LINE_AA)
                        cv2.putText(img, str(intensity), (bbox[0], bbox[1] + 85), font, 1.0, (225, 0, 0), 2, cv2.LINE_AA)

                        # draw circles to track eye behavioral data
                        cv2.circle(img, (annot['eye']['left']['pos_x'], annot['eye']['left']['pos_y']), 20, (255, 0, 0), 2)
                        cv2.circle(img, (annot['eye']['right']['pos_x'], annot['eye']['right']['pos_y']), 20, (0, 0, 255), 2)

            out.write(img)
            # cv2.imwrite('frame{}.png'.format(framenum), img)

            cv2.imshow("img", img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

            framenum += 1
        else:
            break

    # release Video objects when finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()

    print('Saved file {}'.format(fsave))

if __name__ == '__main__':
    vid_in = "~/UTRAdata/movieannotations/irobot/irobot_with_audio/i_robot_new_02.mp4"
    sesdata = aframe.Session('Epilepsy', 'Monitoring', 'e0005AB', 1, 'A')
    dsave = sesdata.create_analysis('EyeBehavioralData/', 'ses')
    annot_in = dsave + "e0005AB_01_A_EyeBehavioralData_Chapter02.pkl"
    fsave = "~/UTRAdata/movieannotations/irobot/irobot_labeled/i_robot_labeled_02.mp4"
    plot_faces(vid_in, annot_in, fsave)

运行上述脚本时,pycharm表示我已成功保存了新的视频文件,并且没有错误代码。但是,当我播放新的视频文件时,它看起来与原始文件完全一样-矩形,圆形和文本尚未绘制到视频文件上。我哪里出错了?

0 个答案:

没有答案