Skip to content

Instantly share code, notes, and snippets.

@peeranatkankham
Last active May 23, 2023 15:05
Show Gist options
  • Save peeranatkankham/3059b0e71fe98ec200dc703b20f8a335 to your computer and use it in GitHub Desktop.
Save peeranatkankham/3059b0e71fe98ec200dc703b20f8a335 to your computer and use it in GitHub Desktop.
import cv2
import requests
# Load the classifiers
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
body_cascade = cv2.CascadeClassifier('haarcascade_fullbody.xml')
# Set up the camera
cap = cv2.VideoCapture(0)
while True:
# Capture the video feed frame by frame
ret, frame = cap.read()
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces and bodies
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
bodies = body_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
# Draw rectangles around the detected faces and bodies
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
for (x, y, w, h) in bodies:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
# Check if any faces or bodies were detected
if len(faces) > 0 or len(bodies) > 0:
print("Person detected!")
# Save the frame with the rectangles drawn
cv2.imwrite('detected_person.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
# Send notification to LINE using LINE Notify API
url = 'https://notify-api.line.me/api/notify'
token = 'Your Token'
headers = {'Authorization': f'Bearer {token}'}
files = {'imageFile': open('detected_person.jpg', 'rb')}
data = {'message': 'Person detected!'}
res = requests.post(url, headers=headers, data=data, files=files)
# Display the resulting frame
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
# Release the capture and close all windows
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment