Elektronik-Labor Projekte Mikrocontroller Raspberry
_______________________________________________________________________________________ | |
|Benötigte Pakete: |
|sudo apt-get install python-opencv --> für Ausgabe Kommandozeile und Tkinter |
|sudo apt-get install python-imaging-tk --> Tkinter |
|sudo apt-get install python-imaging --> Tkinter |
|_______________________________________________________________________________________|
_____________________________________________________________________________
| |
|lsusb-Ausgabe meines Raspberry: |
|Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. |
|Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub |
|Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. |
|Bus 001 Device 008: ID 0ac8:303b Z-Star Microelectronics Corp. ZC0303 Webcam |
|Bus 001 Device 007: ID 046d:c52b Logitech, Inc. Unifying Receiver |
|_______________________________________________________________________________________|
Nun sollte ein Fenster aufgehen und bei geringer Auflösung der Kamera ein ruckelfreies Bild erscheinen. Da ich meinen Raspberry über ssh und xrdp laufen lasse, könnte man schon fast von einer Videoüberwachung über das Netz sprechen.
#! /usr/bin/env python
# -*- coding: utf-8
import cv2
cap = cv2.VideoCapture(-1)
while(True):
retval, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
#web_cam_cv2_tk.py Webcam mit opencv2 und tkinter
#! /usr/bin/env python
# -*- coding: utf-8
import Tkinter as tk
from PIL import Image, ImageTk
import cv2
WIDTH = 320
HEIGHT = 240
CAM = -1
class Cam(tk.Label):
UPDATE_TIME = 10
PROPID_WIDTH = 3
PROPID_HEIGHT = 4
def __init__(self, root, width, height, cam):
tk.Label.__init__(self, root)
self.root = root
self.width = width
self.height = height
self.cam = cv2.VideoCapture(CAM)
def run(self):
if self.cam.isOpened():
self.tk_image = ImageTk.PhotoImage(image = self.resize_image(
Image.fromstring("RGB", (int(self.cam.get(self.PROPID_WIDTH)),
int(self.cam.get(self.PROPID_HEIGHT))), self.cam.read()[1], "raw", "BGR")))
self.config(image = self.tk_image)
else:
self.config(width=20, height=5,
text = "NO CAM", font="Arial 20")
self.after(self.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()
self.root.destroy()
if __name__ == '__main__':
root = tk.Tk()
cam = Cam(root, WIDTH, HEIGHT, CAM)
cam.pack()
cam.run()
root.title("WEB-CAM")
root.protocol("WM_DELETE_WINDOW", cam.release)
root.mainloop()
# stop_motion_cam_raspberry.py --> Zeitrafferaufnahmen
#! /usr/bin/env python
# -*- coding: utf-8
import time
from datetime import timedelta, datetime
import numpy as np
import cv2
stop_time = datetime.now() + timedelta(minutes=10) # bei 10min = ca 25sec
cam = cv2.VideoCapture(-1) # hours u. seconds auch moeglich
writer = cv2.VideoWriter("out.avi", 4, 24, (int(cam.get(3)),
int(cam.get(4))))
old_frame = []
if cam.isOpened():
try:
while datetime.now() < stop_time:
time.sleep(1)
new_frame = cam.read()[1]
if np.array_equal(old_frame, new_frame):
raise RuntimeError("cam disconnected")
writer.write(new_frame)
old_frame = new_frame
except KeyboardInterrupt:
print "capture stopped"
except RuntimeError as error:
print error
else:
print "capture finished"
finally:
cam.release()
writer.release()
else:
print "no cam"
#! /usr/bin/env python_____________________________________Code__________________________________________
# -*- coding: utf-8
import time
import datetime
import numpy as np
from itertools import count
import cv2
SENSITIVITY = 150000
INIT_STEPS = 10
UPDATE_INTERVAL = 0.5
FOLDER = "detected"
cam = cv2.VideoCapture(-1)
counter = 0
init_counter = count()
if cam.isOpened():
try:
while True:
time.sleep(UPDATE_INTERVAL)
frame_1 = cam.read()[-1]
frame_2 = cam.read()[-1]
changes = 0
if np.array_equal(frame_1, frame_2):
raise RuntimeError("cam disconnected")
for old, new in zip(frame_1, frame_2):
changes += np.isclose(old, new).sum()
if counter == INIT_STEPS:
if changes < SENSITIVITY:
print "detected"
cv2.imwrite("{0}/{1:%d%b%Y_%H_%M_%S.%f}.png".format(
FOLDER, datetime.datetime.utcnow()), frame_2)
else:
counter = init_counter.next()
except KeyboardInterrupt:
print "capture stopped"
except RuntimeError as error:
print error
else:
print "capture finished"
finally:
cam.release()
else:
print "no cam"