- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
76 lines (68 loc) · 2.54 KB
/
app.py
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
73
74
75
76
importcv2
importtime
importthreading
fromcv2importcv2
fromPILimportImage, ImageTk
fromtkinterimportLabel, Button, Tk, PhotoImage
classCameraApp:
def__init__(self, window):
self.window=window
self.window.title("My Camera")
self.window.geometry("500x400")
self.window.configure(bg="#ff2fff")
self.window.resizable(1, 1)
Label(self.window, width=400, height=30, bg="black").place(x=0, y=320)
self.TakePhoto_b=Button(self.window, width=20, text="Shot", font= ("Times", 15),bg="#2F4F4F", relief='flat', command=self.TakePhoto)
self.ImageLabel=Label(self.window, width=500, height=320, bg="#4682B4")
self.ImageLabel.place(x=0, y=0)
self.TakePhoto_b.place(x=150, y=360)
self.take_picture=False
self.PictureTaken=False
self.Main()
@staticmethod
defLoadCamera():
camera=cv2.VideoCapture(0)
ifcamera.isOpened():
ret, frame=camera.read()
whileret:
ret, frame=camera.read()
ifret:
yieldframe
else:
yieldFalse
defTakePhoto(self):
ifnotself.PictureTaken:
print('Taking a Picture')
self.take_picture=True
else:
print("Reconfiguring camera")
self.TakePhoto_b.configure(text="Shot")
self.take_picture=False
defMain(self):
self.render_thread=threading.Thread(target=self.StartCamera)
self.render_thread.daemon=True
self.render_thread.start()
defStartCamera(self):
frame=self.LoadCamera()
CaptureFrame=None
whileTrue:
Frame=next(frame)
print(self.take_picture)
ifframeandnotself.take_picture:
picture=Image.fromarray(Frame)
picture=picture.resize((500, 400), resample=0)
CaptureFrame=picture.copy()
picture=ImageTk.PhotoImage(picture)
self.ImageLabel.configure(image=picture)
self.ImageLabel.photo=picture
self.PictureTaken=False
time.sleep(0.001)
else:
ifnotself.PictureTaken:
print("Your camera died")
CaptureFrame.save('myimage.png')
self.TakePhoto_b.configure(text="Take Again")
self.PictureTaken=True
root=Tk()
App=CameraApp(root)
root.mainloop()