Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import requests
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Model URL
|
7 |
+
image_model_url = 'https://teachablemachine.withgoogle.com/models/ZPfAhDYCh/model.json'
|
8 |
+
|
9 |
+
# Load the model
|
10 |
+
net = cv2.dnn.readNetFromTensorflow(requests.get(image_model_url).content)
|
11 |
+
|
12 |
+
# Function to classify the image
|
13 |
+
def classify_image(frame):
|
14 |
+
# Flip the frame horizontally for better classification
|
15 |
+
frame = cv2.flip(frame, 1)
|
16 |
+
|
17 |
+
# Prepare the frame for classification
|
18 |
+
blob = cv2.dnn.blobFromImage(frame, size=(224, 224), swapRB=True, crop=False)
|
19 |
+
net.setInput(blob)
|
20 |
+
|
21 |
+
# Get the predictions
|
22 |
+
predictions = net.forward()
|
23 |
+
|
24 |
+
# Find the index of the class with the highest confidence
|
25 |
+
class_index = np.argmax(predictions)
|
26 |
+
|
27 |
+
# Print the predicted label
|
28 |
+
if class_index == 0:
|
29 |
+
label = 'Class 0'
|
30 |
+
elif class_index == 1:
|
31 |
+
label = 'Class 1'
|
32 |
+
else:
|
33 |
+
label = 'Unknown'
|
34 |
+
|
35 |
+
return label
|
36 |
+
|
37 |
+
# Create Gradio interface
|
38 |
+
iface = gr.Interface(fn=classify_image, inputs="webcam", outputs="text")
|
39 |
+
|
40 |
+
# Launch the interface
|
41 |
+
iface.launch()
|