我需要制作一个在二进制文件(1024x768)中找到5x5帧的程序,以便使其具有时间效率,我需要进行多处理。 由于某种原因,程序跳过了“ p.join()”,并且显示了管道破裂的错误。 为了简化它,我只尝试了一个进程,但想法是使多个进程在同一映像上运行。 帧是5x5,一个位宽,每个位的值在100-110之间,如下所示:
XXXXX
X---X
X---X
X---X
XXXXX
代码:
from multiprocessing import Process, Value,Lock
import os
def frame_finder(picture,start,finnish,V,lock):
for b in range(start,finnish):
frame_flag=1
pixel=bytes2int(picture[b])
if (pixel in range(100,111)):
for i in range(4):
new_pixel_upper_row=bytes2int(picture[b+i+1])
if not(new_pixel_upper_row in range(100,111)):
frame_flag=0
break
new_pixel_lower_row=bytes2int(picture[b+i+1+1024*4])
if not(new_pixel_lower_row in range(100,111)):
frame_flag=0
break
if not frame_flag:
continue
for j in range(3):
new_pixel_left_column=bytes2int(picture[b+(i+1)*1024])
if not(new_pixel_left_column in range(100,111)):
break
new_pixel_right_column=bytes2int(picture[b+(i+1)*1024+4])
if not(new_pixel_right_column in range(100,111)):
break
if (i+j==5):
with lock:
V.value += b+1024*2+2
V.value+= 0
def bytes2int(str):
return int(str.encode('hex'), 16)
os.chdir("D:\\hw2")
num_of_processes=1
lock=Lock()
p_list=list()
V=Value('i',0)
f=open("fr1.bin","rb")
try:
while(f):
picture=f.read(1024*768)
for i in range(num_of_processes):
p_list=[Process(target=frame_finder,args=(picture,i*((1024*768)/num_of_processes),(i+1)*((1024*768)/num_of_processes),V,lock))]
for p in p_list:
p.start()
for p in p_list:
p.join()
finally:
f.close()
print V.value