Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b60ef4

Browse files
Python Camera
0 parents commit 8b60ef4

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

‎app.py‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import cv2
2+
import time
3+
import threading
4+
from PIL import Image, ImageTk
5+
from tkinter import Label, Button, Tk, PhotoImage
6+
7+
8+
class CameraApp:
9+
def __init__(self, window):
10+
self.window = window
11+
self.window.title("My Camera")
12+
self.window.geometry("500x400")
13+
self.window.configure(bg="#ff2fff")
14+
self.window.resizable(1, 1)
15+
Label(self.window, width=400, height = 30, bg="black").place(x=0, y =320)
16+
self.TakePhoto_b = Button(self.window, width = 20, text = "Shot", font = ("Times", 15),bg = "#2F4F4F", relief = 'flat', command=self.TakePhoto)
17+
self.ImageLabel = Label(self.window, width = 500, height= 320, bg = "#4682B4")
18+
self.ImageLabel.place(x=0, y=0)
19+
self.TakePhoto_b.place(x = 150, y = 360)
20+
self.take_picture = False
21+
self.PictureTaken = False
22+
self.Main()
23+
24+
@staticmethod
25+
def LoadCamera():
26+
camera = cv2.VideoCapture(0)
27+
if camera.isOpened():
28+
ret, frame = camera.read()
29+
while ret:
30+
ret, frame = camera.read()
31+
if ret:
32+
yield frame
33+
else:
34+
yield False
35+
36+
def TakePhoto(self):
37+
if not self.PictureTaken:
38+
print('Taking a Picture')
39+
self.take_picture = True
40+
else:
41+
print("Reconfiguring camera")
42+
self.TakePhoto_b.configure(text = "Shot")
43+
self.take_picture = False
44+
45+
def Main(self):
46+
self.render_thread = threading.Thread(target=self.StartCamera)
47+
self.render_thread.daemon = True
48+
self.render_thread.start()
49+
50+
def StartCamera(self):
51+
frame = self.LoadCamera()
52+
CaptureFrame = None
53+
while True:
54+
Frame = next(frame)
55+
print(self.take_picture)
56+
if frame and not self.take_picture:
57+
picture = Image.fromarray(Frame)
58+
picture = picture.resize((500, 400), resample=0)
59+
CaptureFrame = picture.copy()
60+
picture = ImageTk.PhotoImage(picture)
61+
self.ImageLabel.configure(image = picture)
62+
self.PictureTaken = False
63+
time.sleep(0.001)
64+
else:
65+
if not self.PictureTaken:
66+
print("Your camera died")
67+
CaptureFrame.save('myimage.png')
68+
self.TakePhoto_b.configure(text = "Take Again")
69+
self.PictureTaken = True
70+
71+
72+
root = Tk()
73+
App = CameraApp(root)
74+
root.mainloop()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /