首页 > 其他分享 >pySimpleGUI实现照相机功能

pySimpleGUI实现照相机功能

时间:2023-02-19 11:23:38浏览次数:40  
标签:功能 img self cv2 pySimpleGUI window time sg 照相机

做了一个照相机功能,包括拍照和录像,纯粹是为了好玩

import PySimpleGUI as sg
import cv2
import threading

layout = [
    [sg.Text('傻瓜照相机: ')],
    [sg.Image(key="-IMGSRC-",size=(640, 480))],
    [sg.Button('拍照')],
    [sg.Button('录像'), sg.Text('', key='-TIME-'), sg.Button('停止录像', disabled=True)]
]

window = sg.Window('Python GUI', layout, keep_on_top=True)
mutex = threading.Lock()

def compute_time(start_time, end_time):
    return (end_time-start_time)/cv2.getTickFrequency()

def set_window_img(imgbytes):
    if imgbytes is not None:
        window['-IMGSRC-'].update(data=imgbytes)

class CaptureThread(threading.Thread):
    def __init__(self, number=0):
        super().__init__()
        self.camera_id = number
        self.isCapture = True

    def set_iscapture(self, True_or_False):
        mutex.acquire()
        self.isCapture = True_or_False
        mutex.release()

    def get_iscapture(self):
        return self.isCapture

    def run(self):
        cap = cv2.VideoCapture(self.camera_id, cv2.CAP_DSHOW)
        start_time = cv2.getTickCount()
        while self.isCapture:
            ret, img = cap.read()
            if ret:
                img = cv2.resize(img, (640, 480))
                imgbytes = cv2.imencode('.png', img)[1].tobytes()
                if self.get_iscapture():
                    set_window_img(imgbytes)
                    end_time = cv2.getTickCount()
                    elapse = compute_time(start_time, end_time)
                    string = str(int(elapse))+ ' 秒'
                    # window['-TIME-'].update(datetime.now().strftime("%H:%M:%S"))
                    window['-TIME-'].update(string)
        cap.release()
        print('thread capture stopped!')

while True:
    event, values = window.read()
    if event==None:
        break
    if event=='拍照':
        print('take a photo')
        cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
        ret, img = cap.read()
        img = cv2.resize(img, window['-IMGSRC-'].get_size())
        imgbytes = cv2.imencode('.png', img)[1].tobytes()
        window['-IMGSRC-'].update(data=imgbytes)
        cap.release()
    if event=='录像':
        print('record')
        CaptureThr = CaptureThread(0)
        CaptureThr.start()
        # window['-TIME-'].update(datetime.now().strftime("%H:%M:%S"))
        window['停止录像'].update(disabled=False)
    if event=='停止录像':
        CaptureThr.set_iscapture(False)
        window['停止录像'].update(disabled=True)
window.close()

界面如下:

标签:功能,img,self,cv2,pySimpleGUI,window,time,sg,照相机
From: https://www.cnblogs.com/crazyMint/p/17134396.html

相关文章

  • (功能介绍)Kingshard数据库中间件学习【一】
    Kingshard常用功能1.支持读写分离2.支持水平分库分表3.平滑上下线,前端无感4.支持sql黑名单机制(比如:deletefromtable但是没有带where操作,从而删除了整张表的数据,所以......
  • 点赞点踩功能样式准备
    1.在博客园复制其中一篇文章的HTML放到自己页面的内容中   2.如上图解决内容样式问题   结果:   3.添加点赞点踩的数字内容:注:去掉点击事件的代码 ......
  • 给gridview添加checkBox 并且做全选功能
    对gridview添加模板编辑模板-->显示中选择:HeaderTemplate-->把chekcBox拖进来                显示中选择:ItemTemplate-->把chekcBox拖进来 在headerTem......
  • react小案例:发表评论功能
      ////导入包importReactfrom'react'importReactDOM from'react-dom'classAppextendsReact.Component{state={  //数据  comments:[ ......
  • tui.editor一款功能强大的markdown编辑器
    简介最近在捯饬自己的个人网站,想找一款类似于掘金的markdown编辑器,主要诉求包含实时预览、语法高亮、自动生成目录索引。对比了市面上主流的几款编辑器,最后采用了@toast-u......
  • Redis实战12-优惠券实现一人一单功能
    本文收获在上一篇,我们已经把超卖问题解决了。接下来,我们来开发,优惠券一人一单功能。通过本文学习,您将有如下收获:1:悲观锁、乐观锁的使用场景;2:synchronized关键字,在不同位置,锁......
  • //Dotween的一些功能
    //不停的转_vec3.Set(0,0,360000f);DoRotate=loadicon.transform.DOLocalRotate(_vec3,2000f,RotateMode.FastBeyond360).SetEase(Ease.Linear).SetLoops(-1,LoopT......
  • 很简单 但又一时想不起来的功能合集
     ///<summary>///根据模型的尺寸取得的模型边缘位置与模型中心的相对位置(在不适用物理的情况下模型移动式避免穿模)///</summary>varbounds=GetComponent<Mesh......
  • 用指针实现strcpy功能
    1#include<stdio.h>2intmain(intargc,constchar*argv[])3{4chara[45]="helloword";5charb[45]="xjwkkk";6char*p1=a;7cha......
  • c++ 11 lamda 如何实现 linq 中 先 where 再 select 的功能
    面向AI编程,回答如下:在C++11中,可以使用lambda表达式和算法库中的std::copy_if和std::transform算法来实现LINQ中先where再select的功能。具体来说,可以先使用......