Spaces:
Running
Running
Commit
·
856c28f
1
Parent(s):
fde96be
added sentiment.py and object_detection.py
Browse files- safetycam/object_detection.py +47 -0
- safetycam/sentiment.py +37 -3
safetycam/object_detection.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
from deepface import DeepFace
|
3 |
+
|
4 |
+
def get_dominant_emotion(dictionary_list):
|
5 |
+
dominant_emotion = dictionary_list[0]['dominant_emotion']
|
6 |
+
return dominant_emotion
|
7 |
+
|
8 |
+
def obejct_detection():
|
9 |
+
cap=cv2.VideoCapture(0)
|
10 |
+
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
11 |
+
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
|
12 |
+
smile_casacde = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_smile.xml')
|
13 |
+
|
14 |
+
while True:
|
15 |
+
ret, frame = cap.read()
|
16 |
+
try:
|
17 |
+
emotion=DeepFace.analyze(frame, actions= ['emotion'])
|
18 |
+
except:
|
19 |
+
print("No face detected.")
|
20 |
+
to_gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
21 |
+
faces=face_cascade.detectMultiScale(to_gray,1.3,5)
|
22 |
+
for (x,y,w,h) in faces:
|
23 |
+
cv2.rectangle(frame, (x,y), (x+w, y+h), (255,0,0), 2)
|
24 |
+
try:
|
25 |
+
dominant=get_dominant_emotion(emotion)
|
26 |
+
cv2.putText(frame, dominant, (x+10,y+50), cv2.FONT_HERSHEY_COMPLEX, 1 , (0,255,0),2)
|
27 |
+
except:
|
28 |
+
print("Face undetected.")
|
29 |
+
roi_gray=to_gray[y:y+w, x:x+w]
|
30 |
+
roi_color=frame[y:y+h, x:x+w]
|
31 |
+
smiles=smile_casacde.detectMultiScale(roi_gray,1.3,7)
|
32 |
+
for (x_s,y_s,w_s,h_s) in smiles:
|
33 |
+
cv2.rectangle(roi_color,(x_s, y_s),(x_s+w_s,y_s+h_s),(255,0,0),2)
|
34 |
+
cv2.putText(roi_color,"Smile", (x_s+10,y_s+50), cv2.FONT_HERSHEY_COMPLEX, 0.5 , (0,255,0),2)
|
35 |
+
eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
|
36 |
+
for (ex, ey, ew, eh) in eyes:
|
37 |
+
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (255, 0, 0), 5)
|
38 |
+
cv2.putText(roi_color,"Eye", (ex+10,ey+30), cv2.FONT_HERSHEY_COMPLEX, 0.5 , (0,255,0),2)
|
39 |
+
cv2.imshow('frame', frame)
|
40 |
+
|
41 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
42 |
+
break
|
43 |
+
|
44 |
+
cap.release()
|
45 |
+
cv2.destroyAllWindows()
|
46 |
+
|
47 |
+
obejct_detection()
|
safetycam/sentiment.py
CHANGED
@@ -1,4 +1,38 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from nltk.corpus import stopwords
|
3 |
+
from textblob import Word, TextBlob
|
4 |
+
stop_words=stopwords.words('english')
|
5 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
6 |
|
7 |
+
def replace_non_alphanumeric(text):
|
8 |
+
result = ""
|
9 |
+
for char in text:
|
10 |
+
if char.isalnum() or char.isspace():
|
11 |
+
result += char
|
12 |
+
return result
|
13 |
+
|
14 |
+
def preprocess_texts(text):
|
15 |
+
processed_text = replace_non_alphanumeric(text)
|
16 |
+
processed_text = " ".join(word for word in processed_text.split() if word not in stop_words)
|
17 |
+
processed_text = " ".join(Word(word).lemmatize() for word in processed_text.split())
|
18 |
+
return processed_text
|
19 |
+
|
20 |
+
def get_polarity_subjectivity(preprocess_text):
|
21 |
+
processed_text=preprocess_texts(preprocess_text)
|
22 |
+
polarity = TextBlob(processed_text).sentiment[0]
|
23 |
+
subjectivity = TextBlob(processed_text).sentiment[1]
|
24 |
+
return polarity, subjectivity
|
25 |
+
|
26 |
+
def sentiment_analysis(text):
|
27 |
+
processed_text=preprocess_texts(text)
|
28 |
+
sia=SentimentIntensityAnalyzer()
|
29 |
+
sentiment=sia.polarity_scores(text)
|
30 |
+
return sentiment
|
31 |
+
|
32 |
+
# use microphone input in the future.
|
33 |
+
text=input()
|
34 |
+
|
35 |
+
dict_sentiment = (sentiment_analysis(text))
|
36 |
+
score = dict_sentiment['compound']
|
37 |
+
if score < -.3:
|
38 |
+
print("Alert")
|