问题
I want to read/write data from Serial port and record video simultaneously. For this task I am using multiprocessing module in Python. I wrote the following code as a test which runs fine when I run it in a separate script i.e. it records a video and writes to a file.
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
However, when I am using it in my main code, none of the processes gets started. My main code looks like this (Will post the functions if required)
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
Both "Hello 1" and "Hello 2" get printed that means the code is getting executed, but I don't see any video or file getting saved. (The camera LED doesn't turn on at all).
I cannot understand what is going wrong here. Please help.
Functions look like
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()
来源:https://stackoverflow.com/questions/48136805/multiprocessing-code-bug