sandeep03edu

test_camera working

Jun 19th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 19.02 KB | None | 0 0
  1. """
  2. Run classification on Camera, Press ESC to exit the program
  3. For Raspberry PI, please use `import tflite_runtime.interpreter as tflite` instead
  4. """
  5. from tkinter.messagebox import NO
  6. from pygame.locals import *
  7. import pygame.camera
  8. import pygame
  9. import socket
  10. from datetime import datetime, date
  11. from multiprocessing import Process
  12. import threading
  13. import warnings
  14.  
  15. from openpyxl import Workbook
  16. import pyttsx3
  17. import io
  18. from PIL import Image
  19. from pymongo import MongoClient
  20. from pygame import mixer
  21. import pandas as pd
  22. import sys
  23. import time
  24. import numpy as np
  25. import tensorflow.lite as tflite
  26. from tensorflow.keras.preprocessing.image import img_to_array
  27. import tensorflow as tf  # version 1.14
  28. import cv2
  29. import os
  30. import csv
  31. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
  32. warnings.filterwarnings('ignore')
  33.  
  34.  
  35.  
  36. CAMERA_WIDTH = 640
  37. CAMERA_HEIGHT = 480
  38.  
  39.  
  40. def textToSpeech():
  41.     engine = pyttsx3.init()
  42.     voices = engine.getProperty("voices")
  43.     engine.setProperty("voice", voices[1].id)
  44.     engine.setProperty("rate", 220)
  45.     engine.say("Suspecious Activity Detected!")
  46.     engine.runAndWait()
  47.     del engine
  48.  
  49.  
  50. def alert():
  51.     t = threading.Thread(target=textToSpeech)
  52.     t.start()
  53.  
  54.  
  55. def load_labels(label_path):
  56.     r"""Returns a list of labels"""
  57.     with open(label_path, 'r') as f:
  58.         return [line.strip() for line in f.readlines()]
  59.  
  60.  
  61. def load_model(model_path):
  62.     r"""Load TFLite model, returns a Interpreter instance."""
  63.     interpreter = tflite.Interpreter(model_path=model_path)
  64.     interpreter.allocate_tensors()
  65.     return interpreter
  66.  
  67.  
  68. def process_image(interpreter, image, input_index, k=3):
  69.     r"""Process an image, Return top K result in a list of 2-Tuple(confidence_score, _id)"""
  70.     input_data = np.expand_dims(image, axis=0)  # expand to 4-dim
  71.  
  72.     # Process
  73.     interpreter.set_tensor(input_index, input_data)
  74.     interpreter.invoke()
  75.  
  76.     # Get outputs
  77.     output_details = interpreter.get_output_details()
  78.     output_data = interpreter.get_tensor(output_details[0]['index'])
  79.     output_data = np.squeeze(output_data)
  80.  
  81.     # Get top K result
  82.     top_k = output_data.argsort()[-k:][::-1]  # Top_k index
  83.     result = []
  84.     for _id in top_k:
  85.         score = float(output_data[_id] / 255.0)
  86.         result.append((_id, score))
  87.  
  88.     return result
  89.  
  90.  
  91. def display_result(top_result, frame, labels):
  92.     r"""Display top K result in top right corner"""
  93.     font = cv2.FONT_HERSHEY_SIMPLEX
  94.     size = 0.6
  95.     color = (255, 0, 0)  # Blue color
  96.     thickness = 1
  97.  
  98.     for idx, (_id, score) in enumerate(top_result):
  99.         x = 12
  100.         y = 24 * idx + 24
  101.         cv2.putText(frame, '{} - {:0.4f}'.format(labels[_id], score),
  102.                     (x, y), font, size, color, thickness)
  103.  
  104.     cv2.imshow('Image Classification', frame)
  105.  
  106.  
  107. if __name__ == "__main__":
  108.  
  109.     model_path = 'S:\Projects\ObsenceMediaDetector\ocd_module_deployment-realtime_final\scuffle_tm_quant.tflite'
  110.     model_path1 = 'S:\Projects\ObsenceMediaDetector\ocd_module_deployment-realtime_final\obscenetmquant.tflite'
  111.     label_path = 'S:\Projects\ObsenceMediaDetector\ocd_module_deployment-realtime_final\labels_scuffle.txt'
  112.     label_path1 = 'S:\Projects\ObsenceMediaDetector\ocd_module_deployment-realtime_final\labels_obscene.txt'
  113.  
  114.     dummyVideoPath = 'S:\Projects\ObsenceMediaDetector\ocd_module_deployment-realtime_final\demo.mp4'
  115.     cap = cv2.VideoCapture(dummyVideoPath)
  116.     cap.set(cv2.CAP_PROP_FRAME_WIDTH, CAMERA_WIDTH)
  117.     cap.set(cv2.CAP_PROP_FRAME_HEIGHT, CAMERA_HEIGHT)
  118.     cap.set(cv2.CAP_PROP_FPS, 15)
  119.  
  120.     dumRet, dumFrame = cap.read()
  121.     print("DummyRet", dumRet)
  122.  
  123.     interpreter = load_model(model_path)
  124.     interpreter1 = load_model(model_path1)
  125.     labels = load_labels(label_path)
  126.     labels1 = load_labels(label_path1)
  127.  
  128.     input_details = interpreter.get_input_details()
  129.  
  130.     print("Input Details", input_details)
  131.  
  132.     input_details1 = interpreter1.get_input_details()
  133.  
  134.     # Get Width and Height
  135.     input_shape = input_details[0]['shape']
  136.     input_shape1 = input_details1[0]['shape']
  137.     height = input_shape[1]
  138.     height = input_shape1[1]
  139.     width = input_shape[2]
  140.     width = input_shape1[2]
  141.  
  142.     # Get input index
  143.     input_index = input_details[0]['index']
  144.     input_index1 = input_details1[0]['index']
  145.  
  146.     # Process Stream
  147.     while True:
  148.         ret, frame = cap.read()        
  149.         print("Ret", ret)
  150.  
  151.         if(frame is None) :
  152.             continue
  153.  
  154.         image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
  155.         image = image.resize((224, 224))
  156.  
  157.         top_result = process_image(interpreter, image, input_index)
  158.         top_result1 = process_image(interpreter1, image, input_index1)
  159.  
  160.         # folder
  161.         path = "./record"
  162.  
  163.         # Check whether the specified path exists or not
  164.         isExist = os.path.exists(path)
  165.         # print(isExist)
  166.  
  167.         if not isExist:
  168.             os.makedirs(path)
  169.             print("new directory is created!")
  170.  
  171.         if isExist:
  172.             path_w = "./record/weapon"
  173.             isExist = os.path.exists(path_w)
  174.             # print(isExist)
  175.  
  176.         if not isExist:
  177.             os.makedirs(path_w)
  178.             print("new directory is created!")
  179.  
  180.         path_v = "./record/vehicle"
  181.         isExist = os.path.exists(path_v)
  182.         # print(isExist)
  183.  
  184.         if not isExist:
  185.             os.makedirs(path_v)
  186.             print("new directory is created!")
  187.  
  188.         path_f = "./record/face"
  189.         isExist = os.path.exists(path_f)
  190.         # print(isExist)
  191.  
  192.         if not isExist:
  193.             os.makedirs(path_f)
  194.             print("new directory is created!")
  195.  
  196.         path_fb = "./record/fullbody"
  197.         isExist = os.path.exists(path_fb)
  198.         # print(isExist)
  199.  
  200.         if not isExist:
  201.             os.makedirs(path_fb)
  202.             print("new directory is created!")
  203.  
  204.         path_scf = "./record/scfproof"
  205.         isExist = os.path.exists(path_scf)
  206.         # print(isExist)
  207.  
  208.         if not isExist:
  209.             os.makedirs(path_scf)
  210.             print("new directory is created!")
  211.  
  212.         path_obs = "./record/obsproof"
  213.         isExist = os.path.exists(path_obs)
  214.         # print(isExist)
  215.  
  216.         if not isExist:
  217.             os.makedirs(path_obs)
  218.             print("new directory is created!")
  219.  
  220.  
  221.         with open('./record/record.csv', 'wb') as csvfile:
  222.             filewriter = csv.writer(
  223.                 csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
  224.  
  225.         wb = Workbook("./record/record.xlsx")
  226.         wb.save("./record/record.xlsx")
  227.  
  228.         # Timing
  229.         now = datetime.now()
  230.         start_time = 10
  231.         t3 = now.strftime("%Hh%Mm%Ss")
  232.         today = date.today()
  233.         d3 = today.strftime("%B%d%Y")
  234.  
  235.         # client = MongoClient(
  236.         #     "mongodb://localhost:27017")
  237.         # db = client.edgedev
  238.         # obsproof = db.obsproof
  239.         # faceproof = db.faceproof
  240.         # vehicleproof = db.vehicleproof
  241.         # weaponproof = db.weaponproof
  242.         # fullbodyproof = db.fullbodyproof
  243.         # scfproof = db.scfproof
  244.         # wproof = db.wproof
  245.  
  246.         # Prediction mechanism obscene
  247.         string_pred_gen = ['NonObscene', 'Obscene']
  248.         interpreter_obs = load_model(model_path="obscenetmquant.tflite")
  249.         interpreter_obs = tflite.Interpreter(
  250.             model_path="obscenetmquant.tflite")
  251.         interpreter_obs.allocate_tensors()
  252.         input_details_obs = interpreter_obs.get_input_details()
  253.         output_details_obs = interpreter_obs.get_output_details()
  254.         input_shape_obs = input_details_obs[0]['shape']
  255.  
  256.         # Prediction mechanism scuffle
  257.         string_pred_gena = ['NoScuffle', 'Scuffle']
  258.         interpreter_scuf = load_model(model_path="scuffle_tm_quant.tflite")
  259.         interpreter_scuf = tflite.Interpreter(
  260.             model_path="scuffle_tm_quant.tflite")
  261.         interpreter_scuf.allocate_tensors()
  262.         input_details_scuf = interpreter_scuf.get_input_details()
  263.         output_details_scuf = interpreter_scuf.get_output_details()
  264.         input_shape_scuf = input_details_scuf[0]['shape']
  265.        
  266.         # Initializing variables
  267.         input_im = None
  268.         Time = []
  269.         Date = []
  270.         Act = []
  271.         count = 0
  272.         # colors = np.random.uniform(0, 255, size=(len(classes), 3))
  273.         act = "act"
  274.  
  275.         # reading the stream and resizing it from (320x240, 640x480, 1280x720, 1920x1080)
  276.         # "http://192.168.0.2:4747/video")#"dems.mp4"http://192.168.0.2:4747/video or 0,1,2
  277.         # cap = cv2.VideoCapture('S:\Projects\ObsenceMediaDetector\ocd_module_deployment-realtime_final\demo.mp4')
  278.         # cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
  279.         # cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
  280.  
  281.         font = cv2.FONT_HERSHEY_TRIPLEX
  282.         if (cap.isOpened() == False):
  283.             # print("Error reading video file")
  284.             engine = pyttsx3.init()
  285.             engine.say(
  286.                 "Your CCTV camera is not working or turned off! Please check it and get back here!")
  287.             engine.runAndWait()
  288.             sys.exit()
  289.         frame_width = int(cap.get(3))
  290.         frame_height = int(cap.get(4))
  291.         size = (frame_width, frame_height)
  292.  
  293.         # while True:
  294.         _, frame = cap.read()
  295.         input_im = frame
  296.         count = 0
  297.         start_time = 10
  298.         now = datetime.now()
  299.         d3 = today.strftime("%B %d, %Y")
  300.         t3 = now.strftime("%H:%M:%S")
  301.         cv2.putText(input_im, d3, (20, 400), font, 1,
  302.                     (250, 250, 250), 1, cv2.LINE_AA)
  303.         cv2.putText(input_im, t3, (20, 440), font, 1,
  304.                     (250, 250, 250), 1, cv2.LINE_AA)
  305.  
  306.         input_im = cv2.resize(input_im, (224, 224))
  307.         input_im = img_to_array(input_im)
  308.         input_im = np.expand_dims(input_im, axis=0)
  309.         height, width, channels = frame.shape
  310.        
  311.        
  312.         input_data = np.array(input_im)
  313.         input_data=input_data.astype(np.uint8)
  314.         interpreter_obs.set_tensor(
  315.             input_details_obs[0]['index'], input_data)
  316.         interpreter_obs.invoke()
  317.         interpreter_scuf.set_tensor(
  318.             input_details_scuf[0]['index'], input_data)
  319.         interpreter_scuf.invoke()
  320.         output_data_obs = interpreter_obs.get_tensor(
  321.             output_details_obs[0]['index'])
  322.         index_pred_obs = 1 if (output_data_obs[0][1] > 0.5) else 0
  323.         output_data_scuf = interpreter_scuf.get_tensor(
  324.             output_details_scuf[0]['index'])
  325.         ind = 1 if (output_data_scuf[0][1] > 0.75) else 0
  326.         prezic_obs = string_pred_gen[index_pred_obs]
  327.         prezic_scuf = string_pred_gena[ind]
  328.         d3 = today.strftime("%B%d%Y")
  329.         t3 = now.strftime("%Hh%Mm%Ss")
  330.  
  331.         if(prezic_obs == 'Obscene') and (prezic_scuf == 'Scuffle'):
  332.             cv2.putText(frame, prezic_obs, (40, 50), font,
  333.                         2, (0, 0, 255), 5, cv2.LINE_AA)
  334.             cv2.putText(frame, prezic_scuf, (40, 100), font,
  335.                         2, (0, 0, 255), 5, cv2.LINE_AA)
  336.             alert()
  337.             act = "Obscene and Scuffle"
  338.             OSFileName = "record/obsProof/obs_" + \
  339.                 str(d3) + str(t3) + ".jpg"
  340.             cv2.imwrite(OSFileName, frame)
  341.             OSFileName = "record/scfProof/scf_" + \
  342.                 str(d3) + str(t3) + ".jpg"
  343.             cv2.imwrite(OSFileName, frame)
  344.             ima = Image.open(OSFileName)
  345.             image_bytes = io.BytesIO()
  346.             ima.save(image_bytes, format='JPEG')
  347.             image = {
  348.                 'data': image_bytes.getvalue(),
  349.                 'Suspecious Activity': act,
  350.                 'date': d3,
  351.                 'time': t3
  352.             }
  353.             # obsproof.insert_one(image)
  354.             # scfproof.insert_one(image)
  355.  
  356.         elif (prezic_obs == 'Obscene') and (prezic_scuf != 'Scuffle'):
  357.             cv2.putText(frame, prezic_obs, (40, 50), font,
  358.                         2, (0, 0, 255), 5, cv2.LINE_AA)
  359.             cv2.putText(frame, prezic_scuf, (40, 100), font,
  360.                         2, (255, 255, 255), 1, cv2.LINE_AA)
  361.             alert()
  362.             act = "Obscene"
  363.             OFileName = "record/obsProof/obs_" + str(d3) + str(t3) + ".jpg"
  364.             cv2.imwrite(OFileName, frame)
  365.             ima = Image.open(OFileName)
  366.             image_bytes = io.BytesIO()
  367.             ima.save(image_bytes, format='JPEG')
  368.             image = {
  369.                 'data': image_bytes.getvalue(),
  370.                 'Suspecious Activity': act,
  371.                 'date': d3,
  372.                 'time': t3
  373.             }
  374.             # obsproof.insert_one(image)
  375.  
  376.         elif (prezic_obs != 'Obscene') and (prezic_scuf == 'Scuffle'):
  377.             cv2.putText(frame, prezic_obs, (40, 50), font,
  378.                         2, (255, 255, 255), 1, cv2.LINE_AA)
  379.             cv2.putText(frame, prezic_scuf, (40, 100), font,
  380.                         2, (0, 0, 255), 5, cv2.LINE_AA)
  381.             alert()
  382.             act = "Scuffle"
  383.             SFileName = "record/scfProof/scf_" + str(d3) + str(t3) + ".jpg"
  384.             cv2.imwrite(SFileName, frame)
  385.             ima = Image.open(SFileName)
  386.             image_bytes = io.BytesIO()
  387.             ima.save(image_bytes, format='JPEG')
  388.             image = {
  389.                 'data': image_bytes.getvalue(),
  390.                 'Suspecious Activity': act,
  391.                 'date': d3,
  392.                 'time': t3
  393.             }
  394.             # scfproof.insert_one(image)
  395.  
  396.         else:
  397.             cv2.putText(frame, prezic_obs, (20, 30), font,
  398.                         1, (255, 255, 255), 1, cv2.LINE_AA)
  399.             cv2.putText(frame, prezic_scuf, (20, 60), font,
  400.                         1, (255, 255, 255), 1, cv2.LINE_AA)
  401.  
  402.            
  403.         # or (indexes == 0):
  404.         if(prezic_obs == 'Obscene') or (prezic_scuf == 'Scuffle'):
  405.             Time.append(t3)
  406.             Date.append(d3)
  407.             Act.append(act)
  408.             dict = {'Time': Time, 'Date': Date, 'Activity': Act}
  409.             df = pd.DataFrame(dict)
  410.             df.to_csv('record/record.csv')
  411.             df.to_excel('record/recording.xlsx')
  412.             detector = cv2.CascadeClassifier(
  413.                 'haarcascade_frontalface_default.xml')
  414.             faces = detector.detectMultiScale(frame, 1.3, 5, 0)
  415.             for (ex, ey, ew, eh) in faces:
  416.                 roi_color = frame[ey:ey+eh, ex:ex+ew]
  417.                 cv2.rectangle(frame, (ex, ey),
  418.                                 (ex+ew, ey+eh), (0, 255, 0), 2)
  419.                 FFileName = "record/face/faces_" + \
  420.                     str(d3) + str(t3) + ".jpg"
  421.                 cv2.imwrite(FFileName, roi_color)
  422.                 ima = Image.open(FFileName)
  423.                 image_bytes = io.BytesIO()
  424.                 ima.save(image_bytes, format='JPEG')
  425.                 image = {
  426.                     'data': image_bytes.getvalue(),
  427.                     'Suspecious Activity': act,
  428.                     'date': d3,
  429.                     'time': t3
  430.                 }
  431.                 # faceproof.insert_one(image)
  432.  
  433.             # weapon
  434.             w_detector = cv2.CascadeClassifier('gun_cascade.xml')
  435.             weapons = detector.detectMultiScale(frame, 1.3, 5, 0)
  436.             for (ex, ey, ew, eh) in weapons:
  437.                 roi_color = frame[ey:ey+eh, ex:ex+ew]
  438.                 cv2.rectangle(frame, (ex, ey),
  439.                                 (ex+ew, ey+eh), (0, 255, 0), 2)
  440.                 WFileName = "./record/weapon/weapons_" + \
  441.                     str(d3) + str(t3) + ".jpg"
  442.                 cv2.imwrite(WFileName, roi_color)
  443.                 ima = Image.open(WFileName)
  444.                 image_bytes = io.BytesIO()
  445.                 ima.save(image_bytes, format='JPEG')
  446.                 image = {
  447.                     'data': image_bytes.getvalue(),
  448.                     'Suspecious Activity': act,
  449.                     'date': d3,
  450.                     'time': t3
  451.                 }
  452.                 # weaponproof.insert_one(image)
  453.  
  454.             # vehicle
  455.             v_detector = cv2.CascadeClassifier('cascade.xml')
  456.             vehicles = detector.detectMultiScale(frame, 1.3, 5, 0)
  457.             for (ex, ey, ew, eh) in vehicles:
  458.                 roi_color = frame[ey:ey+eh, ex:ex+ew]
  459.                 cv2.rectangle(frame, (ex, ey),
  460.                                 (ex+ew, ey+eh), (0, 255, 0), 2)
  461.                 VFileName = "./record/vehicle/vehicles_" + \
  462.                     str(d3) + str(t3) + ".jpg"
  463.                 cv2.imwrite(VFileName, roi_color)
  464.                 ima = Image.open(VFileName)
  465.                 image_bytes = io.BytesIO()
  466.                 ima.save(image_bytes, format='JPEG')
  467.                 image = {
  468.                     'data': image_bytes.getvalue(),
  469.                     'Suspecious Activity': act,
  470.                     'date': d3,
  471.                     'time': t3
  472.                 }
  473.                 # vehicleproof.insert_one(image)
  474.  
  475.             #    full body
  476.             fb_detector = cv2.CascadeClassifier('haarcascade_fullbody.xml')
  477.             fullbody = detector.detectMultiScale(frame, 1.3, 5, 0)
  478.             for (ex, ey, ew, eh) in fullbody:
  479.                 roi_color = frame[ey:ey+eh, ex:ex+ew]
  480.                 cv2.rectangle(frame, (ex, ey),
  481.                                 (ex+ew, ey+eh), (0, 255, 0), 2)
  482.                 FBFileName = "./record/fullbody/fullbody_" + \
  483.                     str(d3) + str(t3) + ".jpg"
  484.                 cv2.imwrite(FBFileName, roi_color)
  485.                 ima = Image.open(FBFileName)
  486.                 image_bytes = io.BytesIO()
  487.                 ima.save(image_bytes, format='JPEG')
  488.                 image = {
  489.                     'data': image_bytes.getvalue(),
  490.                     'Suspecious Activity': act,
  491.                     'date': d3,
  492.                     'time': t3
  493.                 }
  494.                 # fullbodyproof.insert_one(image)
  495.  
  496.             # face
  497.             face_detector = cv2.CascadeClassifier(
  498.                 'haarcascade_frontalface_default.xml')
  499.             face = detector.detectMultiScale(frame, 1.3, 5, 0)
  500.             for (ex, ey, ew, eh) in face:
  501.                 roi_color = frame[ey:ey+eh, ex:ex+ew]
  502.                 cv2.rectangle(frame, (ex, ey),
  503.                                 (ex+ew, ey+eh), (0, 255, 0), 2)
  504.                 FCFileName = "./record/face/face_" + \
  505.                     str(d3) + str(t3) + ".jpg"
  506.                 cv2.imwrite(FCFileName, roi_color)
  507.                 ima = Image.open(FCFileName)
  508.                 image_bytes = io.BytesIO()
  509.                 ima.save(image_bytes, format='JPEG')
  510.                 image = {
  511.                     'data': image_bytes.getvalue(),
  512.                     'Suspecious Activity': act,
  513.                     'date': d3,
  514.                     'time': t3
  515.                 }
  516.                 # faceproof.insert_one(image)
  517.  
  518.         # cv2.imshow("Live Streaming", frame)
  519.         key = cv2.waitKey(1)
  520.         if key == ord('q') or key == ord('Q'):
  521.             cap.release()
  522.             cv2.destroyAllWindows()
  523.             break
  524.  
  525.         display_result(top_result, frame, labels)
  526.         display_result(top_result1, frame, labels)
  527.  
  528.         key = cv2.waitKey(1)
  529.         if key == 27:  # esc
  530.             break
  531.  
  532.     cap.release()
  533.     cv2.destroyAllWindows()
  534.  
Advertisement
Add Comment
Please, Sign In to add comment