und weiter geht es mit den Opencv - Spielereien.
Code: Alles auswählen
#! /usr/bin/env python
# -*- coding: utf-8
import Tkinter as tk
from PIL import Image, ImageTk
import cv2
TITLE = "BINARY-CAM"
UPDATE_TIME = 10
WIDTH = 640
HEIGHT = 480
RGB = "L"
BGR = "BGR"
MODUS = "raw"
CAM = -1
LABEL_WIDTH = 20
LABEL_HEIGHT = 5
LABEL_TEXT = "NO CAM"
LABEL_FONT = "Arial 20"
PROPID_WIDTH = 3
PROPID_HEIGHT = 4
GRAY = 255
KERNEL = (5, 5)
BLOCK_SIZE = 63
PARAM_1 = 5
class Cam(tk.Label):
def __init__(self, width, height, cam):
tk.Label.__init__(self)
self.width = width
self.height = height
self.capture = True
self.cam = cv2.VideoCapture(CAM)
def start_stop(self):
if self.capture:
self.capture = False
else:
self.capture = True
self.after(UPDATE_TIME, self.run)
def run(self):
if self.cam.isOpened():
self.tk_image = ImageTk.PhotoImage(image = self.resize_image(
Image.fromstring(RGB, (int(self.cam.get(PROPID_WIDTH)),
int(self.cam.get(PROPID_HEIGHT))), cv2.blur(cv2.adaptiveThreshold(
cv2.cvtColor(self.cam.read()[1], cv2.COLOR_BGR2GRAY), GRAY,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, BLOCK_SIZE, PARAM_1),
KERNEL), MODUS)))
self.config(image = self.tk_image)
else:
self.config(width=LABEL_WIDTH, height=LABEL_HEIGHT,
text = LABEL_TEXT, font=LABEL_FONT)
if self.capture:
self.after(UPDATE_TIME, 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()
if __name__ == '__main__':
root = tk.Tk()
cam = Cam(WIDTH, HEIGHT, CAM)
cam.pack()
cam.run()
tk.Button(text="Start/Stop", command=cam.start_stop).pack()
root.title(TITLE)
root.mainloop()
cam.release()