简体   繁体   English

Python图像处理时间过长

[英]Python Image Processing Taking Too Long

I would like to do image processing in Python. 我想用Python进行图像处理。 The problem is that I have a loop that is recording the image data from a camera into a numpy array, but in the loop I am trying to do a correlation of pixel data from the last image to the current image to determine whether or not I need to do further processing. 问题是我有一个循环,将来自摄像机的图像数据记录到一个numpy数组中,但是在循环中,我试图对从最后一张图像到当前图像的像素数据进行关联,以确定是否需要做进一步的处理。 This however is killing the execution speed of the loop which is showing lagged image output. 但是,这会破坏显示滞后图像输出的循环的执行速度。

def gen(camera):
    image = np.zeros([480, 640, 3])
    last_image = np.zeros([480, 640, 3])

    frame_number = 0

    while True:
        frame = camera.get_frame()
        #new code

        if(frame_number % 10 == 0):
            #save the current frame
            image = frame
            #this line takes forever on 480x640x3 sized nd.arrray
            corr = signal.correlate(image, last_image)

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        faces = faceCascade.detectMultiScale(
                    gray,
                    scaleFactor=1.1,
                    minNeighbors=5,
                    minSize=(30, 30),
                    flags=cv2.CASCADE_SCALE_IMAGE
                )

                # Draw a rectangle around the faces
        for (x, y, w, h) in faces:
                    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        #cv2.imshow('Video', frame)
        ret, jpeg = cv2.imencode('.jpg', frame)
        #if cv2.waitKey(1) & 0xFF == ord('q'):
         #   break
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n')
        if(frame_number % 10 == 0):
            #save current frame as last frame for next process 
            last_image = frame

        frame_number += 1

Is multi threading the way to solve this problem? 多线程是解决此问题的方法吗?

I am not sure if it is feasible, but you could make the operation faster if you correlate after converting to grayscale. 我不确定这是否可行,但是如果转换为灰度后进行关联,则可以使操作更快。 This will make the correlation on a lot less data, and might actually be possible with decent FPS. 这将使大量数据之间的相关性降低,并且实际上可以通过适当的FPS来实现。

Another approach might be to correlate each channel independently, after a quick test, it might execute faster. 另一种方法可能是独立关联每个通道,在快速测试后,它可能会执行得更快。

Alternatively, for a multithreaded approach you could correlate each of the RGB images on a different thread. 另外,对于多线程方法,可以将每个RGB图像关联在不同的线程上。 How faster that would be, if faster at all, is left to find out with experiment. 究竟有多快,如果有多快,还可以通过实验找出。 The link from jwpfox's comment might be a good start. jwpfox的评论链接可能是一个好的开始。 However, last I was working with multiprocessing in Python, there was an issue of it copying the memory to each of the child processes. 但是,最后我在Python中使用多处理,这是一个将内存复制到每个子进程的问题。 I do not know if this multiprocessing will do the same, but it is something to keep in mind for a future speed up. 我不知道这种多处理程序是否会执行相同的操作,但为将来的加速而牢记这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM