bosayama commited on
Commit
a1dc0fd
·
verified ·
1 Parent(s): c47d1d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -1
app.py CHANGED
@@ -1 +1,38 @@
1
- pip install opencv-python
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import tensorflow as tf
5
+
6
+ # Teachable MachineモデルのURL
7
+ URL = "https://teachablemachine.withgoogle.com/models/ZPfAhDYCh/"
8
+
9
+ # モデルとメタデータのURL
10
+ model_url = URL + "model.json"
11
+ metadata_url = URL + "metadata.json"
12
+
13
+ # モデルとメタデータのロード
14
+ model = tf.keras.models.load_model(model_url)
15
+ metadata = gr.tfjs.ModelMetadata(metadata_url)
16
+
17
+ # ウェブカメラのキャプチャ
18
+ cap = cv2.VideoCapture(0)
19
+
20
+ # カメラからの入力を処理する関数
21
+ def classify_image(frame):
22
+ # フレームをリサイズしてモデルに適した形式に変換
23
+ input_data = cv2.resize(frame, (200, 200))
24
+ input_data = np.expand_dims(input_data, axis=0)
25
+
26
+ # 画像の予測
27
+ predictions = model.predict(input_data)
28
+
29
+ # 予測結果を表示
30
+ results = {metadata.get_class_label(i): float(predictions[0][i]) for i in range(len(predictions[0]))}
31
+ return results
32
+
33
+ # インターフェースの作成
34
+ iface = gr.Interface(fn=classify_image, inputs="webcam", outputs="label")
35
+
36
+ # インターフェースの起動
37
+ iface.launch()
38
+