Created
July 7, 2018 07:01
-
-
Save akiraak/91b349a8576cbd147504dff401ccc6b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Tkinter as tk | |
import RPi.GPIO as GPIO | |
from datetime import datetime | |
import picamera | |
import os.path | |
import time | |
camera = picamera.PiCamera() | |
camera.resolution = (2592, 1944) | |
camera.rotation = 270 | |
switchGPIO = 4 | |
picturesPath = '/home/pi/projects/camera/output' | |
class FullScreenApp(object): | |
def __init__(self, master, **kwargs): | |
self.master = master | |
pad = 3 | |
self._geom = '200x200+0+0' | |
master.geometry("{0}x{1}+0+0".format( | |
master.winfo_screenwidth() - pad, master.winfo_screenheight() - pad)) | |
master.bind('<Escape>', self.toggle_geom) | |
def toggle_geom(self, event): | |
geom = self.master.winfo_geometry() | |
print(geom, self._geom) | |
self.master.geometry(self._geom) | |
self._geom = geom | |
window = tk.Tk() | |
FullScreenApp(window) | |
window.title("Camera") | |
shotLabel = tk.Label(text='') | |
shotLabel.place(x=564, y=20) | |
shotLabel.config(font=('System', 32)) | |
window.update() | |
def previewCamera(): | |
scale = 0.85 | |
w = int(640 * scale) | |
h = int(480 * scale) | |
camera.start_preview( | |
fullscreen = False, | |
window = (0, 67, w, h)) | |
def initGPIO(): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(switchGPIO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) | |
def cleanGPIO(): | |
GPIO.cleanup() | |
def takePicture(): | |
filename = datetime.now().strftime('%Y%m%d-%H%M%S.jpg') | |
path = os.path.join(picturesPath, filename) | |
camera.capture(path) | |
checkSaved(path) | |
def checkSaved(path): | |
if os.path.exists(path) and os.path.getsize(path) > 0: | |
shotLabel['text'] = 'Saved' | |
else: | |
shotLabel['text'] = 'Error' | |
window.update() | |
time.sleep(3) | |
shotLabel['text'] = '' | |
window.update() | |
def main(): | |
try: | |
initGPIO() | |
previewCamera() | |
swiche = False | |
while True: | |
swicheStatus = GPIO.input(switchGPIO) == GPIO.HIGH | |
if not swiche: | |
if swicheStatus: | |
takePicture() | |
swiche = swicheStatus | |
except KeyboardInterrupt: | |
pass | |
cleanGPIO() | |
camera.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment