多处理代码错误

时间:2018-01-07 11:48:09

标签: python-3.x opencv multiprocessing pyserial

我想从串口读取/写入数据并同时录制视频。对于此任务,我在Python中使用多处理模块。我编写了以下代码作为测试,当我在一个单独的脚本中运行它时运行正常,即它记录视频并写入文件。

video_length = 10

time_stamp = datetime.now().strftime("%d-%m-%y-%H-%M-%S.%f")[:-3] 

i= 5

for i in range(10):
    if i == 5: # Simulate an imaginary condition
        if __name__ == '__main__':
            P1 = Process(target = Record_Video,  args = (video_length,time_stamp, ))
            P2 = Process(target = my_write_file, args = (time_stamp,))
            P1.start(); P2.start()
            P1.join() ; P2.join() # this blocks until the process terminates

但是,当我在主代码中使用它时,没有任何进程启动。 我的主要代码如下所示(如果需要,将发布函数)

while True:

    jj = get_data(n_readings)

    My_radar_data = radar_data(jj, n_readings , confidence)

    s1, s2, s3 = My_radar_data.get_direction()

    print('Direction = ', s1,',' , 'C_a = ',str(s2)[:4],',','C_r',str(s3)[:4])
    print('\n')

    time_stamp = datetime.now().strftime("%y-%m-%d-%H-%M-%S.%f")[:-3]

    if s1 == 'Receding':
        print("Hello 1")
        if __name__ == '__main__':
            print("Hello 2")
            P1 = Process(target = Record_Video,  args = (video_length,time_stamp , ))
            P2 = Process(target = my_write_file, args = (time_stamp ,))
            P1.start(); P2.start()
            P1.join() ; P2.join() # this blocks until the process terminates

“Hello 1”和“Hello 2”都打印出来意味着代码正在执行,但我没有看到任何视频或文件被保存。 (相机LED根本不亮)。

我无法理解这里出了什么问题。请帮忙。

功能看起来像

def Record_Video(video_length, t_stamp):  

    cap = cv2.VideoCapture(0) # Camera is started as soon as a camera obect is initialised

    #Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')

    file_name = t_stamp

    out = cv2.VideoWriter(file_name+'.avi',fourcc, 30, (640,480))

    t1 = datetime.now()

    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret==True:
            frame = cv2.flip(frame,1) # BE CAREFUL ONLY For flipping the webcam images

            # write the flipped frame

            cv2.putText(img = frame, text = datetime.now().strftime("%y-%m-%d-%H-%M-%S.%f")[:-3], 
                        org = (int(640/6 - 20),int(480/6)), # Place where you want text to be 
                        fontFace = cv2.FONT_HERSHEY_DUPLEX,  # Font that you want to put
                        fontScale = 1, color = (0, 0, 0)) # Fontsize and colour scheme is BGR                

            out.write(frame)

            cv2.imshow('frame',frame)

            if (cv2.waitKey(1) & 0xFF == ord('q')) or ((datetime.now()-t1).total_seconds() > video_length):
                break
        else:
            break

    # Release everything if job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()


class file_trial:

    def __init__(self, name): # rd is a list containing radar data

        self.name = name

    def write_file(self):

        time.sleep(5)

        with open(self.name+'.txt', 'w') as file:

            file.write("Hello")


def my_write_file(t_stamp):

    a = file_trial(t_stamp)

    a.write_file()

0 个答案:

没有答案