-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcam_python.py
More file actions
76 lines (53 loc) ยท 1.94 KB
/
cam_python.py
File metadata and controls
76 lines (53 loc) ยท 1.94 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
import cv2
import numpy as np
from urllib.request import urlopen
import tensorflow as tf
url = "http://192.168.0.72:81/stream" # ESP CAM์ ์์ ์คํธ๋ฆฌ๋ฐ ์ฃผ์
stream = urlopen(url)
buffer = b''
# Define the input size of the model
input_size = (224, 224)
# Open the web cam
# cap = cv2.VideoCapture(0) # webcam ๋ถ๋ฌ์ค๊ธฐ
# cap.set(cv2.CAP_PROP_BUFFERSIZE, 5)
# Load the saved model
model = tf.keras.models.load_model("keras_model.h5", compile=False)
while True:
if len(buffer) < 40960:
buffer += stream.read(40960)
head = buffer.find(b'\xff\xd8')
end = buffer.find(b'\xff\xd9')
# print(head, end, len(buffer))
try: # ๊ฐ๋ ๋น์ด์๋ ๋ฒํผ๋ฅผ ๋ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํจ. ์ด๋ฅผ ์ํ try๋ฌธ
if head > -1 and end > -1:
jpg = buffer[head:end + 2]
buffer = buffer[end + 2:]
frame = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
cv2.imshow("stream", frame)
key = cv2.waitKey(1)
except:
print("Hi")
# Resize the frame for the model
model_frame = cv2.resize(frame, input_size, frame)
# Expand Dimension (224, 224, 3) -> (1, 224, 224, 3) and Nomarlize the data
model_frame = np.expand_dims(model_frame, axis=0) / 255.0
# Predict
is_card_prob = model.predict(model_frame)[0]
is_card = np.argmax(is_card_prob)
# Add Information on screen
if is_card == 0:
msg_card = "Card 1"
elif is_card == 1:
msg_card = "Card 2"
elif is_card == 2:
msg_card = "Card 3"
else:
msg_card = "It's not card"
msg_card += " ({:.1f})%".format(is_card_prob[is_card] * 100)
cv2.putText(frame, msg_card, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (225, 0, 0), thickness=2)
# Show the result and frame
cv2.imshow('classification_card', frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()