Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import subprocess
- import numpy as np
- import re
- import cv2
- from tflite_runtime.interpreter import Interpreter
- import os
- import time
- import sys
- import math
- import glob
- import signal
- from datetime import datetime,timedelta
- #import pygame
- from pygame import mixer
- #pygame.mixer.init()
- mixer.init()
- def alert(audio, timer):
- mixer.music.load(audio)
- mixer.music.set_volume(0.7)
- mixer.music.play()
- #pygame.mixer.Sound(audio).play()
- time.sleep(timer)
- #pygame.mixer.Sound(audio).stop()
- #mixer.music.stop()
- time.sleep(0.5)
- print("Audio File Finished Playing")
- check = 1
- # clear ram
- pics = glob.glob('/run/shm/test*.jpg')
- for t in range(0,len(pics)):
- os.remove(pics[t])
- def Camera_start(wx,hx):
- global p
- rpistr = "libcamera-vid -t 0 --segment 1 --codec mjpeg -n -o /run/shm/test%06d.jpg --width " + str(wx) + " --height " + str(hx)
- p = subprocess.Popen(rpistr, shell=True, preexec_fn=os.setsid)
- #initialise variables
- width = 640 #720
- height = 480 #540
- CAMERA_WIDTH = 640
- CAMERA_HEIGHT = 480
- #face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
- #eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
- #open_eyes_detected = 0
- #face_detected = 0
- start = 0
- cv2.namedWindow('Frame')
- Text = "Left Mouse click on picture to EXIT, Right Mouse click for eye detaction ON/OFF"
- ttrat = time.time()
- #define mouse clicks (LEFT to EXIT, RIGHT to switch eye detcetion ON/OFF)
- def mouse_action(event, x, y, flags, param):
- global p,check
- if event == cv2.EVENT_LBUTTONDOWN:
- os.killpg(p.pid, signal.SIGTERM)
- cv2.destroyAllWindows()
- sys.exit()
- if event == cv2.EVENT_RBUTTONDOWN:
- if check == 0:
- check = 1
- else:
- check = 0
- cv2.setMouseCallback('Frame',mouse_action)
- # start capturing images
- Camera_start(width,height)
- def load_labels(path='labels.txt'):
- """Loads the labels file. Supports files with or without index numbers."""
- with open(path, 'r', encoding='utf-8') as f:
- lines = f.readlines()
- labels = {}
- for row_number, content in enumerate(lines):
- pair = re.split(r'[:\s]+', content.strip(), maxsplit=1)
- if len(pair) == 2 and pair[0].strip().isdigit():
- labels[int(pair[0])] = pair[1].strip()
- else:
- labels[row_number] = pair[0].strip()
- return labels
- def set_input_tensor(interpreter, image):
- """Sets the input tensor."""
- tensor_index = interpreter.get_input_details()[0]['index']
- input_tensor = interpreter.tensor(tensor_index)()[0]
- input_tensor[:, :] = np.expand_dims((image-255)/255, axis=0)
- def get_output_tensor(interpreter, index):
- """Returns the output tensor at the given index."""
- output_details = interpreter.get_output_details()[index]
- tensor = np.squeeze(interpreter.get_tensor(output_details['index']))
- return tensor
- def detect_objects(interpreter, image, threshold):
- """Returns a list of detection results, each a dictionary of object info."""
- set_input_tensor(interpreter, image)
- interpreter.invoke()
- # Get all output details
- boxes = get_output_tensor(interpreter, 1)
- classes = get_output_tensor(interpreter, 3)
- scores = get_output_tensor(interpreter, 0)
- count = int(get_output_tensor(interpreter, 2))
- results = []
- for i in range(count):
- if scores[i] >= threshold:
- result = {
- 'bounding_box': boxes[i],
- 'class_id': classes[i],
- 'score': scores[i]
- }
- results.append(result)
- return results
- def main():
- labels = load_labels()
- interpreter = Interpreter('detect.tflite')
- interpreter.allocate_tensors()
- _, input_height, input_width, _ = interpreter.get_input_details()[0]['shape']
- #cap = cv2.VideoCapture(0)
- while True:
- # remove message after 3 seconds
- #if time.time() - ttrat > 3 and ttrat > 0:
- # Text =""
- # ttrat = 0
- # load image
- pics = glob.glob('/run/shm/test*.jpg')
- while len(pics) < 2:
- pics = glob.glob('/run/shm/test*.jpg')
- pics.sort(reverse=True)
- imgf = cv2.imread(pics[1])
- if len(pics) > 2:
- for tt in range(2,len(pics)):
- os.remove(pics[tt])
- #ret, frame = cap.read()
- img = cv2.resize(cv2.cvtColor(imgf, cv2.COLOR_BGR2RGB), (320,320))
- res = detect_objects(interpreter, img, 0.7)
- print(res)
- for result in res:
- ymin, xmin, ymax, xmax = result['bounding_box']
- xmin = int(max(1,xmin * CAMERA_WIDTH))
- xmax = int(min(CAMERA_WIDTH, xmax * CAMERA_WIDTH))
- ymin = int(max(1, ymin * CAMERA_HEIGHT))
- ymax = int(min(CAMERA_HEIGHT, ymax * CAMERA_HEIGHT))
- cv2.rectangle(imgf,(xmin, ymin),(xmax, ymax),(0,255,0),3)
- cv2.putText(imgf,labels[int(result['class_id'])],(xmin, min(ymax, CAMERA_HEIGHT-20)), cv2.FONT_HERSHEY_SIMPLEX, 0.5,(255,255,255),2,cv2.LINE_AA)
- # Website to Convert from Whatsapp voice to [mp3] - https://convertio.co/ogg-mp3/
- if labels[int(result['class_id'])] == 'goodjob':
- print('play SOUND Good Job')
- alert("goodjob.mp3",3)
- if labels[int(result['class_id'])] == 'goodlife':
- print('play SOUND Good Life')
- alert("goodlife.mp3",3)
- if labels[int(result['class_id'])] == 'hello':
- print('play SOUND HELLO')
- alert("hello.mp3",3)
- if labels[int(result['class_id'])] == 'iloveyou':
- print('play SOUND I Love You')
- alert("iloveyou.mp3",3)
- if labels[int(result['class_id'])] == 'notsure':
- print('play SOUND Not Sure')
- alert("notsure.mp3",3)
- if labels[int(result['class_id'])] == 'ok':
- print('play SOUND OK')
- alert("ok.mp3",3)
- if labels[int(result['class_id'])] == 'thisiscool':
- print('play SOUND This Is Cool')
- alert("thisiscool.mp3",3)
- #cv2.imshow('Pi Feed', frame)
- #display image
- cv2.putText(img,Text, (10, height - 10), 0, 0.4, (0, 255, 255))
- cv2.imshow('Frame',imgf)
- cv2.waitKey(10)
- if cv2.waitKey(10) & 0xFF ==ord('q'):
- cap.release()
- cv2.destroyAllWindows()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement