-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcap.py
More file actions
117 lines (92 loc) · 3.02 KB
/
cap.py
File metadata and controls
117 lines (92 loc) · 3.02 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Tkinter as tk
import os
import cv2
import sys
from PIL import Image, ImageTk
import numpy
fileName = os.environ['ALLUSERSPROFILE'] + "\WebcamCap.txt"
cancel = False
def prompt_ok(event = 0):
global cancel, button, button1, button2
cancel = True
button.place_forget()
button1 = tk.Button(mainWindow, text="Good Image!", command=saveAndExit)
button2 = tk.Button(mainWindow, text="Try Again", command=resume)
button1.place(anchor=tk.CENTER, relx=0.2, rely=0.9, width=150, height=50)
button2.place(anchor=tk.CENTER, relx=0.8, rely=0.9, width=150, height=50)
button1.focus()
def saveAndExit(event = 0):
global prevImg
if (len(sys.argv) < 2):
filepath = "imageCap.jpg"
else:
filepath = sys.argv[1]
print ("Output file to: " + filepath)
prevImg.save(filepath)
mainWindow.quit()
def resume(event = 0):
global button1, button2, button, lmain, cancel
cancel = False
button1.place_forget()
button2.place_forget()
mainWindow.bind('<Return>', prompt_ok)
button.place(bordermode=tk.INSIDE, relx=0.5, rely=0.9, anchor=tk.CENTER, width=300, height=50)
lmain.after(10, show_frame)
def changeCam(event=0, nextCam=-1):
global camIndex, cap, fileName
if nextCam == -1:
camIndex += 1
else:
camIndex = nextCam
del(cap)
cap = cv2.VideoCapture(camIndex)
#try to get a frame, if it returns nothing
success, frame = cap.read()
if not success:
camIndex = 0
del(cap)
cap = cv2.VideoCapture(camIndex)
f = open(fileName, 'w')
f.write(str(camIndex))
f.close()
try:
f = open(fileName, 'r')
camIndex = int(f.readline())
except:
camIndex = 0
cap = cv2.VideoCapture(camIndex)
capWidth = cap.get(3)
capHeight = cap.get(4)
success, frame = cap.read()
if not success:
if camIndex == 0:
print("Error, No webcam found!")
sys.exit(1)
else:
changeCam(nextCam=0)
success, frame = cap.read()
if not success:
print("Error, No webcam found!")
sys.exit(1)
mainWindow = tk.Tk(screenName="Camera Capture")
mainWindow.resizable(width=False, height=False)
mainWindow.bind('<Escape>', lambda e: mainWindow.quit())
lmain = tk.Label(mainWindow, compound=tk.CENTER, anchor=tk.CENTER, relief=tk.RAISED)
button = tk.Button(mainWindow, text="Capture", command=prompt_ok)
button_changeCam = tk.Button(mainWindow, text="Switch Camera", command=changeCam)
lmain.pack()
button.place(bordermode=tk.INSIDE, relx=0.5, rely=0.9, anchor=tk.CENTER, width=300, height=50)
button.focus()
button_changeCam.place(bordermode=tk.INSIDE, relx=0.85, rely=0.1, anchor=tk.CENTER, width=150, height=50)
def show_frame():
global cancel, prevImg, button
_, frame = cap.read()
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
prevImg = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=prevImg)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
if not cancel:
lmain.after(10, show_frame)
show_frame()
mainWindow.mainloop()