-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesp32_pycharm.py
More file actions
61 lines (55 loc) ยท 2.25 KB
/
esp32_pycharm.py
File metadata and controls
61 lines (55 loc) ยท 2.25 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
import cv2
import PIL.Image, PIL.ImageTk
from tkinter import *
import numpy as np
from urllib.request import urlopen
class App:
def __init__(self, window):
#ํ๋ฉด ์์ฑ
self.width, self.height = 320, 320
self.window = window
self.window.geometry("320x320")
self.window.title("Read ESP32-CAM")
self.window.bind('<Key>', self.keyPressed)
self.buffer = b''
#esp-cap stream
url = "http://192.168.0.72" #Your url
self.stream = urlopen(url)
self.canvas = Canvas(window, width = self.width, height = self.height)
self.canvas.pack()
self.delay = 1
self.isCaputure = 0
self.update()
self.window.mainloop()
def keyPressed(self, event):
print(event.char)
if event.char == 'a':#ํ์ฌ ํ๋ฉด์ ์ฌ์ง์ผ๋ก ์ ์ฅ
self.isCaputure = 1
if event.char == 'q':#์ข
๋ฃํ๊ธฐ
self.window.destroy()
def update(self):
while True:
#์ดฌ์ ๋ฐ์ดํ ๋ฐ์์ค๊ธฐ
self.buffer += self.stream.read(2560)
head = self.buffer.find(b'\xff\xd8')
end = self.buffer.find(b'\xff\xd9')
try:
if head > -1 and end > -1:
#์ดฌ์ ๋ฐ์ดํ๋ฅผ jpg๋ก ๋ณํํ๊ธฐ
jpg = self.buffer[head:end+2]
self.buffer = self.buffer[end+2:]
img = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (320, 320))
# frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE) # vertical
#์ฌ์ง ํ์ผ๋ก ์ ์ฅํ๊ธฐ
if self.isCaputure:
cv2.imwrite('capture' + ".jpg", img)
self.isCaputure = 0
self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(img))
self.canvas.create_image(0, 0, image = self.photo, anchor = NW)
break
except:
pass
self.window.after(ms=self.delay, func=self.update)
App(Tk())