#!/usr/bin/python如果需要修改一下背景,裡面就有**。# -*- coding: utf-8 -*-
import sys
import cv2
from pyqt5 import qtcore, qtgui, qtwidgets
from pyqt5.qtwidgets import *
from pyqt5.qtcore import *
from pyqt5.qtgui import qpalette, qbrush, qpixmap
import os
class ui_mainwindow(qtwidgets.qwidget):
def __init__(self, parent=none):
super(ui_mainwindow, self).__init__(parent)
# self.face_recong = face.recognition()
self.timer_camera = qtcore.qtimer()
self.cap = cv2.videocapture()
self.cam_num = 0
self.set_ui()
self.slot_init()
self.__flag_work = 0
self.x =0
self.count = 0
def set_ui(self):
self.__layout_main = qtwidgets.qhboxlayout()
self.__layout_fun_button = qtwidgets.qvboxlayout()
self.__layout_data_show = qtwidgets.qvboxlayout()
self.button_open_camera = qtwidgets.qpushbutton(u'開啟相機')
self.button_close = qtwidgets.qpushbutton(u'退出')
#button 的顏色修改
button_color = [self.button_open_camera, self.button_close]
for i in range(2):
button_color[i].setstylesheet("qpushbutton"
"qpushbutton:hover"
"qpushbutton"
"qpushbutton"
"qpushbutton"
"qpushbutton")
self.button_open_camera.setminimumheight(50)
self.button_close.setminimumheight(50)
# move()方法移動視窗在螢幕上的位置到x = 300,y = 300座標。
self.move(500, 500)
# 資訊顯示
self.label_show_camera = qtwidgets.qlabel()
self.label_move = qtwidgets.qlabel()
self.label_move.setfixedsize(100, 100)
self.label_show_camera.setfixedsize(641, 481)
self.label_show_camera.setautofillbackground(false)
self.__layout_fun_button.addwidget(self.button_open_camera)
self.__layout_fun_button.addwidget(self.button_close)
self.__layout_fun_button.addwidget(self.label_move)
self.__layout_main.addlayout(self.__layout_fun_button)
self.__layout_main.addwidget(self.label_show_camera)
self.setlayout(self.__layout_main)
self.label_move.raise_()
self.setwindowtitle(u'攝像頭')
'''# 設定背景
msg = qtwidgets.qmessagebox.warning(self, u"warning", u"請檢測相機與電腦是否連線正確", buttons=qtwidgets.qmessagebox.ok,
defaultbutton=qtwidgets.qmessagebox.ok)
# if msg==qtgui.qmessagebox.cancel:
# pass
else:
self.timer_camera.start(30)
self.button_open_camera.settext(u'關閉相機')
else:
self.timer_camera.stop()
self.cap.release()
self.label_show_camera.clear()
self.button_open_camera.settext(u'開啟相機')
def show_camera(self):
flag, self.image = self.cap.read()
# face = self.face_detect.align(self.image)
# if face:
# pass
show = cv2.resize(self.image, (640, 480))
show = cv2.cvtcolor(show, cv2.color_bgr2rgb)
# print(show.shape[1], show.shape[0])
#show.shape[1] = 640, show.shape[0] = 480
showimage = qtgui.qimage(show.data, show.shape[1], show.shape[0], qtgui.qimage.format_rgb888)
self.label_show_camera.setpixmap(qtgui.qpixmap.fromimage(showimage))
# self.x += 1
# self.label_move.move(self.x,100)
# if self.x ==320:
# self.label_show_camera.raise_()
def closeevent(self, event):
ok = qtwidgets.qpushbutton()
cacel = qtwidgets.qpushbutton()
msg = qtwidgets.qmessagebox(qtwidgets.qmessagebox.warning, u"關閉", u"是否關閉!")
msg.addbutton(ok,qtwidgets.qmessagebox.actionrole)
msg.addbutton(cacel, qtwidgets.qmessagebox.rejectrole)
ok.settext(u'確定')
cacel.settext(u'取消')
# msg.setdetailedtext('sdfsdff')
if msg.exec_() == qtwidgets.qmessagebox.rejectrole:
event.ignore()
else:
# self.socket_client.send_command(self.socket_client.current_user_command)
if self.cap.isopened():
self.cap.release()
if self.timer_camera.isactive():
self.timer_camera.stop()
event.accept()
if __name__ == "__main__":
ex = ui_mainwindow()
ex.show()
**參考
其實裡面的**是同乙個
pyQT編寫介面呼叫攝像頭
1 首先要知道使用命令呼叫攝像頭,並將其寫成乙個槽函式,這樣方便以後增加新操作 def prepcamera self self.camera cv2.videocapture 0 2 下面編寫乙個簡單介面,我將要呼叫攝像頭的qlabel放在下圖選中的位置,當點選開始按鈕時,能呼叫開啟攝像頭的操作,...
5呼叫攝像頭拍照 手機攝像頭越多拍照越好嗎?
1999年開始,手機進入了可以拍照的時代,雖然現在看來,110萬畫素的攝像頭,總共能儲存20張 實在是有些 簡陋 本文中所說的 三攝 四攝 都說的是後置攝像頭 攝像頭多 拍照好 今天的手機市場上,最主流的就是三攝配置,然後四攝的手機也越來越多,估計你們也聽過 這個手機有三攝,拍照可好了 這樣的介紹,...
使用DirectShow驅動攝像頭
對於小攝像頭的驅動有幾種方法,通過使用directshow來驅動攝像頭靈活性比較好,有簡單的方法也有比較複雜但更有效的方法,本文只介紹簡單方法,希望與大家交流!用directshow來使用攝像頭,一般要求攝像頭的驅動是wdm格式的,當然,一些比較老的驅動格式directshow也可支援。direct...