Spaces:
Runtime error
Runtime error
First Commit
Browse files- app.py +181 -0
- frecog/haarcascade_eye.xml +0 -0
- frecog/haarcascade_frontalface_default.xml +0 -0
- frecog/haarcascade_fullbody.xml +0 -0
- frecog/haarcascade_smile.xml +0 -0
- image.jpg +0 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import DetrFeatureExtractor, DetrForObjectDetection
|
2 |
+
import requests
|
3 |
+
import torch
|
4 |
+
|
5 |
+
feature_extractor = DetrFeatureExtractor.from_pretrained("facebook/detr-resnet-50")
|
6 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
|
7 |
+
|
8 |
+
|
9 |
+
# Core Pkgs
|
10 |
+
import time
|
11 |
+
from json import load
|
12 |
+
import streamlit as st
|
13 |
+
import cv2
|
14 |
+
from PIL import Image,ImageEnhance
|
15 |
+
import numpy as np
|
16 |
+
from io import BytesIO
|
17 |
+
from transformers import pipeline
|
18 |
+
st.set_page_config(page_title="Do Transform Images", initial_sidebar_state = "auto" )
|
19 |
+
st.title("Face Detection App")
|
20 |
+
st.text("Build with Streamlit and OpenCV")
|
21 |
+
|
22 |
+
face_cascade = cv2.CascadeClassifier('frecog/haarcascade_frontalface_default.xml')
|
23 |
+
eye_cascade = cv2.CascadeClassifier('frecog/haarcascade_eye.xml')
|
24 |
+
smile_cascade = cv2.CascadeClassifier('frecog/haarcascade_smile.xml')
|
25 |
+
obj_detector = pipeline('object-detection')
|
26 |
+
|
27 |
+
def detect_faces(our_image):
|
28 |
+
new_img = np.array(our_image.convert('RGB'))
|
29 |
+
img = cv2.cvtColor(new_img,1)
|
30 |
+
gray = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY)
|
31 |
+
# Detect faces
|
32 |
+
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
33 |
+
# Draw rectangle around the faces
|
34 |
+
for (x, y, w, h) in faces:
|
35 |
+
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
36 |
+
return img,faces
|
37 |
+
def detect_eyes(our_image):
|
38 |
+
new_img = np.array(our_image.convert('RGB'))
|
39 |
+
img = cv2.cvtColor(new_img,1)
|
40 |
+
gray = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY)
|
41 |
+
eyes = eye_cascade.detectMultiScale(gray, 1.3, 5)
|
42 |
+
for (ex,ey,ew,eh) in eyes:
|
43 |
+
cv2.rectangle(img,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
|
44 |
+
return img
|
45 |
+
|
46 |
+
def detect_smiles(our_image):
|
47 |
+
new_img = np.array(our_image.convert('RGB'))
|
48 |
+
img = cv2.cvtColor(new_img,1)
|
49 |
+
gray = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY)
|
50 |
+
# Detect Smiles
|
51 |
+
smiles = smile_cascade.detectMultiScale(gray, 1.1, 4)
|
52 |
+
# Draw rectangle around the Smiles
|
53 |
+
for (x, y, w, h) in smiles:
|
54 |
+
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
|
55 |
+
return img
|
56 |
+
|
57 |
+
def cartonize_image(our_image):
|
58 |
+
new_img = np.array(our_image.convert('RGB'))
|
59 |
+
img = cv2.cvtColor(new_img,1)
|
60 |
+
gray = cv2.cvtColor(new_img, cv2.COLOR_BGR2GRAY)
|
61 |
+
# Edges
|
62 |
+
gray = cv2.medianBlur(gray, 5)
|
63 |
+
edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 9)
|
64 |
+
#Color
|
65 |
+
color = cv2.bilateralFilter(img, 9, 300, 300)
|
66 |
+
#Cartoon
|
67 |
+
cartoon = cv2.bitwise_and(color, color, mask=edges)
|
68 |
+
|
69 |
+
return cartoon
|
70 |
+
|
71 |
+
|
72 |
+
def cannize_image(our_image):
|
73 |
+
new_img = np.array(our_image.convert('RGB'))
|
74 |
+
img = cv2.cvtColor(new_img,1)
|
75 |
+
img = cv2.GaussianBlur(img, (11, 11), 0)
|
76 |
+
canny = cv2.Canny(img, 100, 150)
|
77 |
+
return canny
|
78 |
+
def detect_objects(im):
|
79 |
+
inputs = feature_extractor(images=im, return_tensors="pt")
|
80 |
+
outputs = model(**inputs)
|
81 |
+
# convert outputs (bounding boxes and class logits) to COCO API
|
82 |
+
target_sizes = torch.tensor([im.size[::-1]])
|
83 |
+
results = feature_extractor.post_process(outputs, target_sizes=target_sizes)[0]
|
84 |
+
boxes = []
|
85 |
+
f=None
|
86 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
87 |
+
box = [round(i, 2) for i in box.tolist()]
|
88 |
+
# let's only keep detections with score > 0.9
|
89 |
+
if score > 0.9:
|
90 |
+
st.success(
|
91 |
+
f"Detected {model.config.id2label[label.item()]} with confidence "
|
92 |
+
f"{round(score.item(), 3)} at location {box}"
|
93 |
+
)
|
94 |
+
boxes.append(box)
|
95 |
+
new_img = np.array(im.convert('RGB'))
|
96 |
+
img = cv2.cvtColor(new_img,1)
|
97 |
+
for (x, y, w, h) in boxes:
|
98 |
+
cv2.rectangle(img,(int(x),int(y)),(int(w), int(h)), (0, 0, 255))
|
99 |
+
return st.image(img)#st.image(box)
|
100 |
+
|
101 |
+
@st.cache
|
102 |
+
def load_image(img):
|
103 |
+
im = Image.open(img)
|
104 |
+
return im
|
105 |
+
activities = ["Detection","About"]
|
106 |
+
choice = st.sidebar.selectbox("Select Activty",activities)
|
107 |
+
def change_photo_state():
|
108 |
+
st.session_state["photo"]="done"
|
109 |
+
uploaded_photo = st.file_uploader("Upload Image",type=['jpg','png','jpeg'], on_change=change_photo_state)
|
110 |
+
camera_photo = st.camera_input("Take a photo", on_change=change_photo_state)
|
111 |
+
if "photo" not in st.session_state:
|
112 |
+
st.session_state["photo"]="not done"
|
113 |
+
if choice == 'Detection':
|
114 |
+
st.subheader("Process your images ...")
|
115 |
+
if st.session_state["photo"]=="done":
|
116 |
+
if uploaded_photo:
|
117 |
+
our_image= load_image(uploaded_photo)
|
118 |
+
if camera_photo:
|
119 |
+
our_image= load_image(camera_photo)
|
120 |
+
if uploaded_photo==None and camera_photo==None:
|
121 |
+
our_image=load_image("image.jpg")
|
122 |
+
enhance_type = st.sidebar.radio("Enhance Type",["Original","Gray-Scale","Contrast","Brightness","Blurring"])
|
123 |
+
if enhance_type == 'Gray-Scale':
|
124 |
+
new_img = np.array(our_image.convert('RGB'))
|
125 |
+
img = cv2.cvtColor(new_img,1)
|
126 |
+
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
|
127 |
+
# st.write(new_img)
|
128 |
+
st.image(gray)
|
129 |
+
elif enhance_type == 'Contrast':
|
130 |
+
c_rate = st.sidebar.slider("Contrast",0.5,3.5)
|
131 |
+
enhancer = ImageEnhance.Contrast(our_image)
|
132 |
+
img_output = enhancer.enhance(c_rate)
|
133 |
+
st.image(img_output)
|
134 |
+
elif enhance_type == 'Brightness':
|
135 |
+
c_rate = st.sidebar.slider("Brightness",0.5,3.5)
|
136 |
+
enhancer = ImageEnhance.Brightness(our_image)
|
137 |
+
img_output = enhancer.enhance(c_rate)
|
138 |
+
st.image(img_output)
|
139 |
+
elif enhance_type == 'Blurring':
|
140 |
+
new_img = np.array(our_image.convert('RGB'))
|
141 |
+
blur_rate = st.sidebar.slider("Brightness",0.5,3.5)
|
142 |
+
img = cv2.cvtColor(new_img,1)
|
143 |
+
blur_img = cv2.GaussianBlur(img,(11,11),blur_rate)
|
144 |
+
st.image(blur_img)
|
145 |
+
elif enhance_type == 'Original':
|
146 |
+
st.image(our_image,width=300)
|
147 |
+
|
148 |
+
else:
|
149 |
+
st.image(our_image,width=300)
|
150 |
+
# Face Detection
|
151 |
+
task = ["Faces","Smiles","Eyes","Cannize","Cartonize","detect_objects"]
|
152 |
+
feature_choice = st.sidebar.selectbox("Find Features",task)
|
153 |
+
if st.button("Process"):
|
154 |
+
if feature_choice == 'Faces':
|
155 |
+
result_img,result_faces = detect_faces(our_image)
|
156 |
+
st.image(result_img)
|
157 |
+
|
158 |
+
st.success("Found {} faces".format(len(result_faces)))
|
159 |
+
elif feature_choice == 'Smiles':
|
160 |
+
result_img = detect_smiles(our_image)
|
161 |
+
st.image(result_img)
|
162 |
+
elif feature_choice == 'Eyes':
|
163 |
+
with st.spinner('Wait for it...'):
|
164 |
+
time.sleep(5)
|
165 |
+
result_img = detect_eyes(our_image)
|
166 |
+
st.image(result_img)
|
167 |
+
|
168 |
+
elif feature_choice == 'Cartonize':
|
169 |
+
result_img = cartonize_image(our_image)
|
170 |
+
st.image(result_img)
|
171 |
+
elif feature_choice == 'Cannize':
|
172 |
+
result_canny = cannize_image(our_image)
|
173 |
+
st.image(result_canny)
|
174 |
+
elif feature_choice == 'detect_objects':
|
175 |
+
detect_objects(our_image)
|
176 |
+
|
177 |
+
elif choice == 'About':
|
178 |
+
st.subheader("About Face Detection App")
|
179 |
+
st.markdown("Built with Streamlit by [Soumen Sarker](https://soumen-sarker-personal-site.streamlitapp.com/)")
|
180 |
+
st.markdown("Credit [here](https://huggingface.co/models?pipeline_tag=object-detection)")
|
181 |
+
#st.success("Isshor Saves @Soumen Sarker")
|
frecog/haarcascade_eye.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|
frecog/haarcascade_frontalface_default.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|
frecog/haarcascade_fullbody.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|
frecog/haarcascade_smile.xml
ADDED
The diff for this file is too large to render.
See raw diff
|
|
image.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Pillow
|
2 |
+
streamlit
|
3 |
+
opencv-python
|
4 |
+
transformers
|
5 |
+
torch
|