File size: 1,326 Bytes
ddc21f0 55de059 d9afba7 55de059 ddc21f0 990c881 ddc21f0 c820e77 990c881 ddc21f0 d9afba7 ddc21f0 d9afba7 990c881 d9afba7 ddc21f0 d9afba7 990c881 ddc21f0 d9afba7 990c881 55de059 990c881 |
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 |
from fastai.vision.all import *
import gradio as gr
import cv2
learn = load_learner('resnet18_model.pkl')
categories = ('Black', 'White')
def classify_image(image, webcam=None):
if image is not None:
is_people, _, probs = learn.predict(PILImage.create(image))
return dict(zip(categories, map(float, probs)))
elif webcam is not None:
is_people, _, probs = learn.predict(PILImage.create(webcam))
return dict(zip(categories, map(float, probs)))
def capture_image(cam):
cam = cv2.VideoCapture(0)
_, frame = cam.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cam.release()
return frame
def webcam():
cam = cv2.VideoCapture(0)
while True:
_, frame = cam.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
yield frame
image = gr.inputs.Image(shape=(192, 192), label="Upload Image", source="upload")
webcam = gr.inputs.Image(shape=(192, 192), label="Webcam Image", source="webcam")
label = gr.outputs.Label()
examples = [['example1.jpg'], ['example2.jpg'], ['example3.jpg']] # Nested list for multiple input components
intf = gr.Interface(fn=classify_image, inputs=[image, webcam], outputs=label, examples=examples,
allow_flagging='never') # Set allow_flagging parameter as a string
intf.launch()
|