缩放网络摄像头Gui

2024-09-29 21:40:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还比较陌生。 我有一个从网络摄像头,我需要放大和缩小,并显示它的实时视频。我需要一个图形用户界面,因为我会添加更多的按钮。 我已经设法做放大,但不是创建一个图形用户界面,它创建了一个数字。 谢谢你的帮助

#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()

Tags: importsizereturnvideopltcv2frameint

热门问题