-
Notifications
You must be signed in to change notification settings - Fork 1
/
Upload_Picture.py
96 lines (81 loc) · 2.74 KB
/
Upload_Picture.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import cv2
from PIL import Image, ImageTk
from tkinter import filedialog
from tkinter import *
# import matplotlib.pyplot as plt
import time
import numpy as np
"""
This module has 3 functions to upload the picture using different methods.
"""
def upload_from_local():
"""
Function is called to upload file from local drive
:return: A string. The path of the file uploaded.
"""
filename = filedialog.askopenfilename(
initialdir=r'C:\Users\admin\Pictures', title='Select a Image File'
)
return filename
def take_picture(filename):
"""
Takes one picture from the default camera.
:param filename: a string
:return: Just the name of the file that is saved in working directory
, The size of the image taken
"""
cam = cv2.VideoCapture(0)
cv2.namedWindow("Take an image hitting Enter, Hit Esc to go out")
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("Take an image hitting Enter, Hit Esc to go out", frame)
k = cv2.waitKey(1)
if k % 256 == 27:
print("Escape hit, closing...")
break
if k % 256 == 13:
# SPACE pressed
img_name = "{}_{}.png".format(filename, img_counter)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
img_counter += 1
cam.release()
cv2.destroyAllWindows()
return img_name, ( np.array(frame).shape[1], np.array(frame).shape[0])
def take_picture_frames_in_video(filename):
"""
Opens the camera and takes a video (does not record). Also Takes frames at discrete times and
returns the frame.
:param filename: a string
:return: 2 objects. a generator which yields a camera frame as an array, and also the
cv2.VideoCapture object
"""
cam = cv2.VideoCapture(0)
cv2.namedWindow("Take an image hitting Enter, Hit Esc to go out")
img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("Take an image hitting Enter, Hit Esc to go out", frame)
time.sleep(1 / 5)
k = cv2.waitKey(1)
if k % 256 == 27:
print("Escape hit, closing...")
img_name = "{}.png".format(filename)
cv2.imwrite(img_name, frame)
print("{} written!".format(img_name))
break
img_name = "{}_{}.png".format(filename, img_counter)
print("{} array created!".format(img_name))
img_counter += 1
yield frame , cam
cam.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
upload_from_local()