简体   繁体   English

Python-以低分辨率显示实时视频供稿,以高分辨率记录静止图像

[英]Python - Display live video feed at low resolution, record a still image at a high resolution

I am reasonably inexperienced Python user (v2.7.8) and I am trying to write a GUI that displays an image from a USB camera at a low 640x480 resolution but at the click of a button records a still at the highest resolution of 2592x1944. 我是没有经验的Python用户(v2.7.8),我正在尝试编写一个GUI,以640x480的低分辨率显示来自USB摄像机的图像,但是单击按钮时仍然以2592x1944的最高分辨率记录图像。 The GUI is being written in Tkinter. GUI是用Tkinter编写的。 My Tkinter program uses Multiprocessing and Queue to take a frame from the camera, store it in queue and display the frame in the object window. 我的Tkinter程序使用“多重处理和队列”从相机中获取一帧,将其存储在队列中并在对象窗口中显示该帧。 I have had the program working on a Raspberry Pi with PiCamera as a proof of principle but now I need to move the system onto a PC with this USB device ( https://www.leopardimaging.com/uploads/LI-OV5640-USB-72_datasheet.pdf ). 我已经有使用PiCamera在Raspberry Pi上运行该程序的经验证明,但是现在我需要将该系统移动到具有此USB设备的PC上( https://www.leopardimaging.com/uploads/LI-OV5640-USB -72_datasheet.pdf )。 I have tried to port the code using OpenCV2 to control the camera, but as soon as I try to alter the resolution of the camera the program hangs with no error message appearing. 我试图使用OpenCV2来移植代码来控制相机,但是一旦我尝试更改相机的分辨率,程序就会挂起,而不会出现错误消息。

I can write similar code in the Python shell and get it working, although my method for displaying a smaller live feed is clunky: 我可以在Python shell中编写类似的代码并使它工作,尽管我显示较小的实时供稿的方法很笨拙:

import numpy as np
from multiprocessing import Process, Queue
from Queue import Empty
import cv2
from PIL import Image, ImageTk
import time
feed_queue = Queue(maxsize=2**4)
live_feed = cv2.VideoCapture(1)
live_feed.set(3,2592)
live_feed.set(4,1944)
live_feed.set(5,15)
while True:
    ret, img = live_feed.read()
    preview_size = (640,480)
    preview_frame = cv2.resize(img, preview_size, interpolation=cv2.INTER_AREA)
    cv2.imshow("input", preview_frame)
    key = cv2.waitKey(10)
    if key == 27:
        cv2.imwrite("output.jpg",img)
        break

However, as soon as I write something similar for working in Tkinter, it all falls down: 但是,一旦我为Tkinter编写了类似的东西,一切都会崩溃:

def image_capture(feed_queue):
    live_feed = cv2.VideoCapture(1)
    live_feed.set(3,2592)
    live_feed.set(4,1944)
    live_feed.set(5,15)

    while True:
        try:
            flag, frame = live_feed.read()
            if flag == 0:
                break
            feed_queue.put(frame)
            cv2.waitKey(10)
        except:
            live_feed.release()
            continue

def update_image(image_label, feed_queue):
    frame = feed_queue.get()
    preview_size = (640,480)
    preview_frame = cv2.resize(frame, preview_size, interpolation = cv2.INTER_AREA)  
    # Convert the colour space information from CV2's BGR to the more standard RGB for use with the Tkinter widgets using Python Image Library built in functions
    im = cv2.cvtColor(preview_frame, cv2.COLOR_BGR2RGB)
    # Create a reference copy of the original image so that it isn't designated as garbage by Python's memory allocator
    a = Image.fromarray(im)
    # Now convert this duplicate image to the format required by Tkinter and assign it to the Tkinter label widget, named image_label
    b = ImageTk.PhotoImage(image=a)
    image_label.configure(image=b)
    image_label._image_cache = b
    # Update the main window with this new image
    root.update()

def update_all(root, image_label, feed_queue):
    update_image(image_label, feed_queue)  
    root.after(0, func=lambda: update_all(root, image_label, feed_queue))


if __name__ == '__main__':
    feed_queue = Queue(maxsize=2**4)

    p = Process(target=image_capture, args=(feed_queue,))
    p.start()

    # setup the update callback
    root.after(0, func=lambda: update_all(root, image_label, feed_queue))
    print 'root.after was called...'
    root.mainloop()
    print 'mainloop exit'
    p.join()

This may not be the most efficient way of doing it, so I would be grateful for any suggestions on better methods or ways I can get this program working. 这可能不是最有效的方法,所以对于任何更好的方法或使程序正常工作的建议,我将不胜感激。

Thanks everyone! 感谢大家!

NOTE: I may have isolated the issue to putting the image into the queue. 注意:我可能已经将图像放入队列的问题孤立了。 I wrote a few commands in the shell code and it crashed at that point 我在shell代码中写了一些命令,然后它崩溃了

import numpy as np
from multiprocessing import Process, Queue
from Queue import Empty
import cv2
global live_feed
live_feed = cv2.VideoCapture(1)
live_feed.set(3,2592
live_feed.set(4,1944)
live_feed.set(5,15)

def takeapicture():
    global live_feed
    flag, frame = live_feed.read()
    return flag, frame

def makeaq(feed_q):
    global live_feed
    flag, frame = live_feed.read()
    feed_q.put(frame)

feed_q = Queue(maxsize = 2**4)
issue, img = takeapicture()
issue, img.shape
>(True, (1944L, 2592L, 3L))
pin = feed_q.get()

And crash! 和崩溃!

Although not technically an answer to this question, I have come up with a solution that lets me do what I need to do. 尽管从技术上讲并不是这个问题的答案,但我想出了一个解决方案,可以让我做需要做的事情。 Python 2.7 CV2 & Tkinter with webcam 带网络摄像头的Python 2.7 CV2和Tkinter

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

相关问题 高分辨率图像使用数组删除低分辨率栅格中的属性 - High resolution image to remove attributes in low resolution raster using array 将高分辨率的网格数据重新栅格化为低分辨率的较粗糙数据? - Regridding high resolution gridded data to low resolution coarser resolution data? Python 中的高分辨率定时器 - High Resolution Timer in Python 以高分辨率填充海洋以隐藏底图中的低分辨率等高线 - Fill oceans in high resolution to hide low resolution contours in basemap 用pytesseract读取低分辨率图像 - Reading low resolution image with pytesseract Qt 设计器 - 使用高分辨率。 需要低分辨率版本 - Qt Designer - Using High Resolution. Need Low Resolution version 使用 matplotlib 和 python 以极低分辨率存储图像的解决方法 - Workaround for storing an image in very low resolution with matplotlib and python 使用Opencv和python从高分辨率图像中检测并裁剪图像 - Detect and crop the image from high resolution image using Opencv with python Python OpenCV 视频分辨率 - Python OpenCV video resolution 如何在Python中显示决策树的高分辨率图片? - How to display high resolution picture of the decision tree in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM