Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
import streamlit as st
|
6 |
+
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
|
7 |
+
|
8 |
+
# Load AI Model from Hugging Face
|
9 |
+
model_name = "microsoft/resnet-50"
|
10 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
11 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
12 |
+
|
13 |
+
def process_frame(frame):
|
14 |
+
# Convert frame to tensor
|
15 |
+
inputs = feature_extractor(images=frame, return_tensors="pt")
|
16 |
+
outputs = model(**inputs)
|
17 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
18 |
+
return predictions
|
19 |
+
|
20 |
+
def main():
|
21 |
+
st.title("Smart Mirror AI")
|
22 |
+
run = st.checkbox("Run Camera")
|
23 |
+
cap = cv2.VideoCapture(0)
|
24 |
+
frame_placeholder = st.empty()
|
25 |
+
|
26 |
+
while run:
|
27 |
+
success, frame = cap.read()
|
28 |
+
if not success:
|
29 |
+
st.write("Failed to capture video")
|
30 |
+
break
|
31 |
+
else:
|
32 |
+
predictions = process_frame(frame)
|
33 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
34 |
+
frame_placeholder.image(frame, channels="RGB")
|
35 |
+
cap.release()
|
36 |
+
|
37 |
+
if __name__ == "__main__":
|
38 |
+
main()
|
39 |
+
|
40 |
+
# requirements.txt
|
41 |
+
opencv-python
|
42 |
+
numpy
|
43 |
+
torch
|
44 |
+
transformers
|
45 |
+
streamlit
|