Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. from imutils.video import VideoStream
  2. import argparse
  3. import datetime
  4. import imutils
  5. import time
  6. import cv2
  7. import numpy as np
  8. import re
  9. import socks
  10. import requests
  11. import telethon
  12. from telethon.tl.functions.contacts import ResolveUsernameRequest
  13. from telethon.tl.functions.channels import GetFullChannelRequest
  14. from telethon.tl.functions.messages import GetFullChatRequest
  15. from telethon.tl.types import InputChannel
  16. from telethon import TelegramClient, events, sync
  17. from telethon.tl.types import PeerUser, PeerChat, PeerChannel
  18. from telethon.tl.types import InputPeerEmpty
  19.  
  20. api_id = 1112222
  21. api_hash = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
  22. phone = "+79xxxxxxxxx"
  23. chat = "YOUR_CHAT"
  24.  
  25. client = TelegramClient('coma1', api_id, api_hash)
  26. client.start()
  27. client.sign_in(phone)
  28.  
  29. ap = argparse.ArgumentParser()
  30. ap.add_argument("-v", "--video", help="path to the video file")
  31. ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
  32. args = vars(ap.parse_args())
  33.  
  34. if args.get("video", None) is None:
  35.     vs = VideoStream(src=0).start()
  36.     time.sleep(2.0)
  37.  
  38. else:
  39.     vs = cv2.VideoCapture(args["video"])
  40.  
  41. firstFrame = None
  42. count = 0
  43.  
  44. while True:
  45.     frame = vs.read()
  46.     frame = frame if args.get("video", None) is None else frame[1]
  47.     text = "Unoccupied"
  48.     if frame is None:
  49.         break
  50.     frame = imutils.resize(frame, width=500)
  51.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  52.     gray = cv2.GaussianBlur(gray, (21, 21), 0)
  53.     if firstFrame is None:
  54.         firstFrame = gray
  55.         continue
  56.     frameDelta = cv2.absdiff(firstFrame, gray)
  57.     thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
  58.     thresh = cv2.dilate(thresh, None, iterations=2)
  59.     cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
  60.         cv2.CHAIN_APPROX_SIMPLE)
  61.     cnts = imutils.grab_contours(cnts)
  62.     for c in cnts:
  63.         if cv2.contourArea(c) < args["min_area"]:
  64.             continue
  65.         (x, y, w, h) = cv2.boundingRect(c)
  66.         cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  67.         text = "Occupied"
  68.         count = count + 1
  69.         cv2.imwrite("frame%d.jpg" % count, frame)
  70.         destination_channel_username=chat
  71.         entity=client.get_entity(destination_channel_username)
  72.         client.send_file(entity=entity,file="frame%d.jpg" % count )
  73.         time.sleep(1)
  74.     if count == 20:
  75.         break
  76.     cv2.putText(frame, "Room Status: {}".format(text), (10, 20),
  77.         cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
  78.     cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
  79.         (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
  80.     cv2.imshow("Security Feed", frame)
  81.     cv2.imshow("Thresh", thresh)
  82.     cv2.imshow("Frame Delta", frameDelta)
  83.     key = cv2.waitKey(1) & 0xFF
  84.     if key == ord("q"):
  85.         break
  86. vs.stop() if args.get("video", None) is None else vs.release()
  87. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement