-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcamera.py
More file actions
201 lines (146 loc) · 7.77 KB
/
camera.py
File metadata and controls
201 lines (146 loc) · 7.77 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import cv2
import tkinter as tk
from tkinter import *
from PIL import Image,ImageTk
from datetime import datetime
from tkinter import messagebox, filedialog
# Defining CreateWidgets() function to create necessary tkinter widgets
def createwidgets():
root.feedlabel = Label(root, bg="steelblue", fg="white", text="WEBCAM FEED", font=('Comic Sans MS',20))
root.feedlabel.grid(row=1, column=1, padx=10, pady=10, columnspan=2)
root.cameraLabel = Label(root, bg="steelblue", borderwidth=3, relief="groove")
root.cameraLabel.grid(row=2, column=1, padx=10, pady=10, columnspan=2)
root.saveLocationEntry = Entry(root, width=55, textvariable=destPath)
root.saveLocationEntry.grid(row=3, column=1, padx=10, pady=10)
root.browseButton = Button(root, width=10, text="BROWSE", command=destBrowse)
root.browseButton.grid(row=3, column=2, padx=10, pady=10)
root.captureBTN = Button(root, text="CAPTURE", command=Capture, bg="LIGHTBLUE", font=('Comic Sans MS',15), width=20)
root.captureBTN.grid(row=4, column=1, padx=10, pady=10)
root.CAMBTN = Button(root, text="STOP CAMERA", command=StopCAM, bg="LIGHTBLUE", font=('Comic Sans MS',15), width=13)
root.CAMBTN.grid(row=4, column=2)
root.previewlabel = Label(root, bg="steelblue", fg="white", text="IMAGE PREVIEW", font=('Comic Sans MS',20))
root.previewlabel.grid(row=1, column=4, padx=10, pady=10, columnspan=2)
root.imageLabel = Label(root, bg="steelblue", borderwidth=3, relief="groove")
root.imageLabel.grid(row=2, column=4, padx=10, pady=10, columnspan=2)
root.openImageEntry = Entry(root, width=55, textvariable=imagePath)
root.openImageEntry.grid(row=3, column=4, padx=10, pady=10)
root.openImageButton = Button(root, width=10, text="BROWSE", command=imageBrowse)
root.openImageButton.grid(row=3, column=5, padx=10, pady=10)
# Calling ShowFeed() function
ShowFeed()
# Defining ShowFeed() function to display webcam feed in the cameraLabel;
def ShowFeed():
# Capturing frame by frame
ret, frame = root.cap.read()
if ret:
# Flipping the frame vertically
frame = cv2.flip(frame, 1)
# Displaying date and time on the feed
cv2.putText(frame, datetime.now().strftime('%d/%m/%Y %H:%M:%S'), (20,30), cv2.FONT_HERSHEY_DUPLEX, 0.5, (0,255,255))
# Changing the frame color from BGR to RGB
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
# Creating an image memory from the above frame exporting array interface
videoImg = Image.fromarray(cv2image)
# Creating object of PhotoImage() class to display the frame
imgtk = ImageTk.PhotoImage(image = videoImg)
# Configuring the label to display the frame
root.cameraLabel.configure(image=imgtk)
# Keeping a reference
root.cameraLabel.imgtk = imgtk
# Calling the function after 10 milliseconds
root.cameraLabel.after(10, ShowFeed)
else:
# Configuring the label to display the frame
root.cameraLabel.configure(image='')
def destBrowse():
# Presenting user with a pop-up for directory selection. initialdir argument is optional
# Retrieving the user-input destination directory and storing it in destinationDirectory
# Setting the initialdir argument is optional. SET IT TO YOUR DIRECTORY PATH
destDirectory = filedialog.askdirectory(initialdir="YOUR DIRECTORY PATH")
# Displaying the directory in the directory textbox
destPath.set(destDirectory)
def imageBrowse():
# Presenting user with a pop-up for directory selection. initialdir argument is optional
# Retrieving the user-input destination directory and storing it in destinationDirectory
# Setting the initialdir argument is optional. SET IT TO YOUR DIRECTORY PATH
openDirectory = filedialog.askopenfilename(initialdir="YOUR DIRECTORY PATH")
# Displaying the directory in the directory textbox
imagePath.set(openDirectory)
# Opening the saved image using the open() of Image class which takes the saved image as the argument
imageView = Image.open(openDirectory)
# Resizing the image using Image.resize()
imageResize = imageView.resize((640, 480), Image.ANTIALIAS)
# Creating object of PhotoImage() class to display the frame
imageDisplay = ImageTk.PhotoImage(imageResize)
# Configuring the label to display the frame
root.imageLabel.config(image=imageDisplay)
# Keeping a reference
root.imageLabel.photo = imageDisplay
# Defining Capture() to capture and save the image and display the image in the imageLabel
def Capture():
# Storing the date in the mentioned format in the image_name variable
image_name = datetime.now().strftime('%d-%m-%Y %H-%M-%S')
# If the user has selected the destination directory, then get the directory and save it in image_path
if destPath.get() != '':
image_path = destPath.get()
# If the user has not selected any destination directory, then set the image_path to default directory
else:
messagebox.showerror("ERROR", "NO DIRECTORY SELECTED TO STORE IMAGE!!")
# Concatenating the image_path with image_name and with .jpg extension and saving it in imgName variable
imgName = image_path + '/' + image_name + ".jpg"
# Capturing the frame
ret, frame = root.cap.read()
# Displaying date and time on the frame
cv2.putText(frame, datetime.now().strftime('%d/%m/%Y %H:%M:%S'), (430,460), cv2.FONT_HERSHEY_DUPLEX, 0.5, (0,255,255))
# Writing the image with the captured frame. Function returns a Boolean Value which is stored in success variable
success = cv2.imwrite(imgName, frame)
# Opening the saved image using the open() of Image class which takes the saved image as the argument
saved_image = Image.open(imgName)
# Creating object of PhotoImage() class to display the frame
saved_image = ImageTk.PhotoImage(saved_image)
# Configuring the label to display the frame
root.imageLabel.config(image=saved_image)
# Keeping a reference
root.imageLabel.photo = saved_image
# Displaying messagebox
if success :
messagebox.showinfo("SUCCESS", "IMAGE CAPTURED AND SAVED IN " + imgName)
# Defining StopCAM() to stop WEBCAM Preview
def StopCAM():
# Stopping the camera using release() method of cv2.VideoCapture()
root.cap.release()
# Configuring the CAMBTN to display accordingly
root.CAMBTN.config(text="START CAMERA", command=StartCAM)
# Displaying text message in the camera label
root.cameraLabel.config(text="OFF CAM", font=('Comic Sans MS',70))
def StartCAM():
# Creating object of class VideoCapture with webcam index
root.cap = cv2.VideoCapture(0)
# Setting width and height
width_1, height_1 = 640, 480
root.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width_1)
root.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height_1)
# Configuring the CAMBTN to display accordingly
root.CAMBTN.config(text="STOP CAMERA", command=StopCAM)
# Removing text message from the camera label
root.cameraLabel.config(text="")
# Calling the ShowFeed() Function
ShowFeed()
# Creating object of tk class
root = tk.Tk()
# Creating object of class VideoCapture with webcam index
root.cap = cv2.VideoCapture(0)
# Setting width and height
width, height = 640, 480
root.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
root.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Setting the title, window size, background color and disabling the resizing property
root.title("Pycam")
root.geometry("1340x700")
root.resizable(True, True)
root.configure(background = "sky blue")
# Creating tkinter variables
destPath = StringVar()
imagePath = StringVar()
createwidgets()
root.mainloop()