läuft bei mir nur so
Code: Alles auswählen
#! /usr/bin/env python
# -*- coding: utf-8
from __future__ import absolute_import, division, print_function
import Tkinter as tk
import cv2
from PIL import Image, ImageTk
CAPTURE_WIDTH = 640
CAPTURE_HEIGHT = 480
DEFAULT_CAM_ID = -1
class CamUI(tk.Label):
UPDATE_INTERVAL = 10
PROPID_WIDTH = 3
PROPID_HEIGHT = 4
def __init__(self, parent, width, height, cam_id):
tk.Label.__init__(self, parent)
self.parent = parent
self.width = width
self.height = height
self.after_id = None
self.image = None
self.cam = cv2.VideoCapture(cam_id)
def start_stop(self):
if self.after_id is None:
self.run()
else:
self.after_cancel(self.after_id)
self.after_id = None
def run(self):
if self.cam.isOpened():
self.image = ImageTk.PhotoImage(
image=self.resize_image(
Image.fromstring(
'L',
(
int(self.cam.get(self.PROPID_WIDTH)),
int(self.cam.get(self.PROPID_HEIGHT))
),
cv2.blur(
cv2.adaptiveThreshold(
cv2.cvtColor(
self.cam.read()[1], cv2.COLOR_BGR2GRAY
),
255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
63,
5,
),
(5, 5)),
'raw',
)
)
)
self.config(image=self.image)
else:
self.config(width=20, height=5, text='NO CAM', font='Arial 20')
self.after_id = self.after(self.UPDATE_INTERVAL, self.run)
def resize_image(self, img):
img_width, img_height = img.size
if self.height >= self.width:
img_height = img_height * self.width // img_width
img_width = self.width
else:
img_width = img_width * self.height // img_height
img_height = self.height
return img.resize((img_width, img_height))
def release(self):
self.cam.release()
self.parent.destroy()
def main():
root = tk.Tk()
root.title('BINARY-CAM')
cam_ui = CamUI(root, CAPTURE_WIDTH, CAPTURE_HEIGHT, DEFAULT_CAM_ID)
cam_ui.pack()
tk.Button(text='Start/Stop', command=cam_ui.start_stop).pack()
cam_ui.run()
root.protocol("WM_DELETE_WINDOW", cam_ui.release)
root.mainloop()
if __name__ == '__main__':
main()