1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| from tkinter import *
from tkinter import messagebox
from tkinter.ttk import Combobox # Importez Combobox depuis ttk
import cv2
from PIL import Image, ImageTk
def open_camera():
global vid
global source
framewidth = 640
frameheight = 480
source = source_video.get() #recupere la valeur du combobox
if not source or source== "":
source=0
source= int(source)
print("source video--------------->: " + str(source))
vid = cv2.VideoCapture(source) # Ouvre la caméra par défaut
vid.set(3, framewidth)
vid.set(4, frameheight)
vid.set(10, 100) # Brightness id = 10 and 100 intensity level
# Fonction pour fermer la caméra
def close_camera():
global vid
if vid is not None and vid.isOpened():
vid.release()
# Fonction pour ouvrir la caméra
def update():
if vid is not None:
ret, frame = vid.read()
if ret :
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
photo = ImageTk.PhotoImage(image=Image.fromarray(frame))
canvas.create_image(0, 0, image=photo, anchor=NW)
canvas.photo = photo
root.after(10, update) # mise a jour toute les 10 ms
# Initialisation de Tkinter
root = Tk()
root.title("Test camera")
root.bind('<Escape>', lambda e: root.quit()) #fermer avec echap
# Bouton pour ouvrir la caméra
button1 = Button(root, text="Open Camera", command=open_camera)
button1.pack()
# Bouton pour fermer la caméra
button1 = Button(root, text="Close Camera", command=close_camera)
button1.pack()
# ajout combobox pour selectionner la source video
options = ["0", "1", "2", "3", "4", "5"]
source_video = StringVar()
combobox_source_video = Combobox(root, state='readonly', textvariable=source_video, values=options)
combobox_source_video.pack()
#fentre principale en 700x600
root.geometry("700x600")
canvas = Canvas(root, width=640, height=480, bg='yellow')
canvas.pack()
delay = 10 # Met à jour la vidéo toutes les 10 millisecondes
vid = None # Variable pour stocker la capture vidéo
root.after(delay, update)
# Lancer l'interface Tkinter
root.mainloop() |
Partager