变焦网络摄像头-Gui

时间:2019-08-26 09:29:47

标签: python python-3.x user-interface opencv3.0

我对Python相当陌生。 我有一个来自网络摄像头的实时视频,我需要放大和缩小并显示它。我需要一个GUI,因为我将添加更多按钮。 我设法进行了放大,但是创建了一个GUI,而是创建了一个图形。 谢谢您的帮助。

#import packages
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import tkinter as tk
from tkinter import *
from tkinter import ttk
import cv2

boxSize = 150
enlargeBy = 3
#getBOX coordinates
def getBoxCoordinates(cap, size):
 width = cap.get(3)
 height = cap.get(4)
 x1 = int(width / 2) - int(size / 2)
 y1 = int(height / 2) - int(size / 2)
 x2 = int(width / 2) + int(size / 2)
 y2 = int(height / 2) + int(size / 2)

 return [(x1, y1), (x2, y2)] #return coordinates

def getBox(cap, boxSize, frame, enlargeBy):
 [(x1, y1), (x2, y2)] = getBoxCoordinates(cap, boxSize);

 # Get pixels in box
 box_img = frame[y1 + 1:y2, x1 + 1:x2]  # +1 cuz it excludes initial 
 pixel interval
 return cv2.resize(box_img, None, fx=enlargeBy, fy=enlargeBy,
                  interpolation=cv2.INTER_LINEAR)  #return resized

cap = cv2.VideoCapture(0); #open webcam
ret, frame = cap.read()

figWidth = 20
figHeight = 8
fig = plt.Figure(figsize=(figWidth, figHeight))


enlarged = getBox(cap, boxSize, frame, enlargeBy)
[(x1, y1), (x2, y2)] = getBoxCoordinates(cap, boxSize);


video_plot = plt.subplot2grid((figHeight, figWidth), (0, 0), colspan=4, 
rowspan=4)
video_plot.axis('off')
video_plot.set_title("Camera feed")
video = video_plot.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
rectangle = plt.Rectangle((x1,y1), x2-x1, y2-y1, edgecolor="gold", 
fill=False)
video_plot.add_patch(rectangle)

#update fig
def updatefig(i):
 ret, frame = cap.read()

 video.set_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

 return [video]

ani = animation.FuncAnimation(fig, updatefig, interval=20, frames=200, 
blit=True)

plt.tight_layout()
plt.show() 

cv2.destroyAllWindows()

0 个答案:

没有答案
相关问题