Spaces:
Build error
Build error
Download app.py from AniruthAppu/Google-Clone-using-streamlit: direct link, hf CLI and curl.
- Browser
- Download file 4.73 kB
-
https://huggingface.co/spaces/AniruthAppu/Google-Clone-using-streamlit/resolve/main/app.py
- Command line
-
hf download hf://spaces/AniruthAppu/Google-Clone-using-streamlit/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AniruthAppu/Google-Clone-using-streamlit/resolve/main/app.py
4.73 kB
| import streamlit as st | |
| import cv2 | |
| import numpy as np | |
| # Load models | |
| faceProto = "opencv_face_detector.pbtxt" | |
| faceModel = "opencv_face_detector_uint8.pb" | |
| ageProto = "age_deploy.prototxt" | |
| ageModel = "age_net.caffemodel" | |
| genderProto = "gender_deploy.prototxt" | |
| genderModel = "gender_net.caffemodel" | |
| MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746) | |
| ageList = ['(0-2)', '(4-6)', '(8-12)', '(15-20)', '(25-32)', '(38-43)', '(48-53)', '(60-100)'] | |
| genderList = ['Male', 'Female'] | |
| # Load networks | |
| faceNet = cv2.dnn.readNet(faceModel, faceProto) | |
| ageNet = cv2.dnn.readNet(ageModel, ageProto) | |
| genderNet = cv2.dnn.readNet(genderModel, genderProto) | |
| # Highlight face function | |
| def highlightFace(net, frame, conf_threshold=0.7): | |
| frameHeight = frame.shape[0] | |
| frameWidth = frame.shape[1] | |
| blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300), [104, 117, 123], True, False) | |
| net.setInput(blob) | |
| detections = net.forward() | |
| faceBoxes = [] | |
| for i in range(detections.shape[2]): | |
| confidence = detections[0, 0, i, 2] | |
| if confidence > conf_threshold: | |
| x1 = int(detections[0, 0, i, 3] * frameWidth) | |
| y1 = int(detections[0, 0, i, 4] * frameHeight) | |
| x2 = int(detections[0, 0, i, 5] * frameWidth) | |
| y2 = int(detections[0, 0, i, 6] * frameHeight) | |
| faceBoxes.append([x1, y1, x2, y2]) | |
| return faceBoxes | |
| # Streamlit app | |
| st.title("Age and Gender Detection") | |
| st.write("Upload an image or use your webcam to detect age and gender.") | |
| # Option to upload an image or use webcam | |
| option = st.radio("Choose Input Method", ("Upload Image", "Use Webcam")) | |
| if option == "Upload Image": | |
| uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file: | |
| # Convert uploaded image to OpenCV format | |
| file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8) | |
| image = cv2.imdecode(file_bytes, 1) | |
| # Process the image | |
| faceBoxes = highlightFace(faceNet, image) | |
| for faceBox in faceBoxes: | |
| face = image[max(0, faceBox[1]-20): min(faceBox[3]+20, image.shape[0]-1), | |
| max(0, faceBox[0]-20): min(faceBox[2]+20, image.shape[1]-1)] | |
| blob = cv2.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False) | |
| # Gender prediction | |
| genderNet.setInput(blob) | |
| genderPreds = genderNet.forward() | |
| gender = genderList[genderPreds[0].argmax()] | |
| # Age prediction | |
| ageNet.setInput(blob) | |
| agePreds = ageNet.forward() | |
| age = ageList[agePreds[0].argmax()] | |
| # Draw rectangle and text | |
| cv2.rectangle(image, (faceBox[0], faceBox[1]), (faceBox[2], faceBox[3]), (0, 255, 0), 2) | |
| label = f"{gender}, {age}" | |
| cv2.putText(image, label, (faceBox[0], faceBox[1]-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2) | |
| # Display output image | |
| st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), channels="RGB", caption="Processed Image") | |
| elif option == "Use Webcam": | |
| run_webcam = st.button("Start Webcam") | |
| if run_webcam: | |
| video = cv2.VideoCapture(0) | |
| stframe = st.empty() # Placeholder for video frames | |
| while True: | |
| ret, frame = video.read() | |
| if not ret: | |
| st.error("Error accessing webcam.") | |
| break | |
| faceBoxes = highlightFace(faceNet, frame) | |
| for faceBox in faceBoxes: | |
| face = frame[max(0, faceBox[1]-20): min(faceBox[3]+20, frame.shape[0]-1), | |
| max(0, faceBox[0]-20): min(faceBox[2]+20, frame.shape[1]-1)] | |
| blob = cv2.dnn.blobFromImage(face, 1.0, (227, 227), MODEL_MEAN_VALUES, swapRB=False) | |
| # Gender prediction | |
| genderNet.setInput(blob) | |
| genderPreds = genderNet.forward() | |
| gender = genderList[genderPreds[0].argmax()] | |
| # Age prediction | |
| ageNet.setInput(blob) | |
| agePreds = ageNet.forward() | |
| age = ageList[agePreds[0].argmax()] | |
| # Draw rectangle and text | |
| cv2.rectangle(frame, (faceBox[0], faceBox[1]), (faceBox[2], faceBox[3]), (0, 255, 0), 2) | |
| label = f"{gender}, {age}" | |
| cv2.putText(frame, label, (faceBox[0], faceBox[1]-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 255), 2) | |
| # Display the video frame | |
| stframe.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), channels="RGB") | |
| video.release() | |