opencv_sample/capture.py
2019-08-10 14:14:15 +03:00

79 lines
2.4 KiB
Python

import os
import cv2
import time
import pandas
from datetime import datetime
import telebot
import config
bot = telebot.TeleBot(config.TOKEN)
prev_frame = None
times = []
# df = pandas.DataFrame(columns=["start", "end"])
video = cv2.VideoCapture(0)
status_new = 0
counter = 0
low_red = (176, 132, 97)
high_red = (209, 151, 114)
face_cascade = cv2.CascadeClassifier("smile.xml")
while True:
status_new, status_old = 0, status_new
check, frame = video.read()
only_cat = cv2.inRange(frame, low_red, high_red)
# cat_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if prev_frame is None:
prev_frame = gray
# continue
delta_frame = cv2.absdiff(prev_frame, gray)
prev_frame = gray
thresh_delta = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_frame = cv2.dilate(thresh_delta, None, iterations=2)
(_, cnts, _) = cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(len(cnts))
for contour in cnts:
if cv2.contourArea(contour) < 1000:
continue
status_new, status_old = 1, status_new
(x, y, w, h) = cv2.boundingRect(contour)
# cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 3)
# status_list.append(status)
if status_new != status_old:
# times.append(datetime.now())
print("=" * 10)
file_name = config.PATH + config.FILE_NAME.format(counter)
cv2.imwrite(file_name, frame)
# send to telegram chat and rmove from disk
bot.send_photo(config.CHAT_ID, open(file_name, "rb"))
os.remove(file_name)
counter += 1
# cv2.imshow("capt", gray)
# cv2.imshow("delta", delta_frame)
# cv2.imshow("threshold", thresh_frame)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=10)
# for x, y, w, h in faces:
# img = cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)
# print(type(faces))
# cv2.imshow("face_detector", img)
cv2.imshow("cat", only_cat)
cv2.imshow("rectangle", frame)
key = cv2.waitKey(1)
if key == ord("q"): # quit
break
#
# for t in range(0, len(times), 2):
# df = df.append({"start": times[t], "end": times[t+1]}, ignore_index=True)
#
# df.to_csv("times.csv")
video.release()
cv2.destroyAllWindows