Spaces:
Build error
Build error
File size: 1,766 Bytes
7c70a38 348e46a 7c70a38 b8d6bec 7c70a38 7e3eb15 7c70a38 7e3eb15 7c70a38 7e3eb15 348e46a 7e3eb15 7c70a38 7e3eb15 348e46a |
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 |
import cv2
import numpy as np
import torch
import streamlit as st
from PIL import Image # Import Image from PIL
from transformers import AutoModelForImageClassification, AutoFeatureExtractor
# Load AI Model from Hugging Face
model_name = "microsoft/resnet-50"
model = AutoModelForImageClassification.from_pretrained(model_name)
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
def process_frame(frame):
# Convert frame to tensor
inputs = feature_extractor(images=[frame], return_tensors="pt")
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
return predictions
def main():
st.title("Smart Mirror AI")
# Option to run camera or upload image
run = st.checkbox("Run Camera")
uploaded_image = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
if run:
cap = cv2.VideoCapture(0)
frame_placeholder = st.empty()
while run:
success, frame = cap.read()
if not success:
st.write("Failed to capture video")
break
else:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
predictions = process_frame(frame)
frame_placeholder.image(frame, channels="RGB")
cap.release()
elif uploaded_image is not None:
# If an image is uploaded, process it
image = Image.open(uploaded_image) # Use PIL.Image to open the image
image = np.array(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
predictions = process_frame(image)
st.image(image, channels="BGR")
st.write(f"Predictions: {predictions}")
if __name__ == "__main__":
main()
|