Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save harveyslash/96124f0db9f29025279fb43e8603635b to your computer and use it in GitHub Desktop.
Save harveyslash/96124f0db9f29025279fb43e8603635b to your computer and use it in GitHub Desktop.
from __future__ import print_function
import cv2
import json
import numpy as np
import socket
import sys
import pickle
import os
from time import sleep
import time
import re
from datetime import datetime
from subprocess import call
import pycurl
import requests
from datetime import timedelta
import struct ### new code
cap=cv2.VideoCapture(1)
cap.set(3,224)
cap.set(4,224)
cap.set(cv2.cv.CV_CAP_PROP_FPS, 60)
url_exp = "http://107.23.213.161/expiration_database.json"
url_add = "http://107.23.213.161/addItem.php"
clientsocket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
clientsocket.connect(('ai.student.rit.edu',8083))
def getRequest(url, food, type):
r = requests.get(url)
json = r.json()
lower_food = food.lower()
filtered = {}
if lower_food not in json.keys():
for key in json:
regex = r"" + re.escape(lower_food)
need_match = re.search(regex, key)
if need_match is not None:
filtered[need_match.group(0)] = json[key]
return filtered[need_match.group(0)][type]
return json[lower_food][type]
"""
Compute Expiration Date
"""
def computeDateLeft(date, food, type):
today = datetime.now()
data = getRequest(url_exp, food, type)
dateleft = ""
if type == 2 and data == "" or data == "\u00a0":
return "Unable to get value"
elif data == "" or data == "\u00a0":
return computeDateLeft(today, food, type+1)
elif "month" in data or "months" in data:
week = int(data[0])*365/12
print("Month: " + str(week))
dateleft = date + timedelta(week)
elif "day" in data or "days" in data:
dateleft = date + timedelta(days=int(data[0]))
elif "year" in data or "years" in data:
year = int(data[0])*365
dateleft = date + timedelta(int(year))
elif "week" in data or "weeks" in data:
week = int(data[0])
dateleft = date + timedelta(week)
return dateleft
def postData(buffer1):
link = "http://107.23.213.161/addItem.php"
print(requests.post(link,buffer1))
def getAveragePoint(pointArr):
sumX = 0
sumY = 0
for point in pointArr:
sumX += point[0]
sumY += point[1]
X= sumX/len(pointArr)
Y= sumY/len(pointArr)
return [X,Y]
pointsBuffer = []
max_conf_object = ""
last_time_of_interest = datetime.now()
while True:
ret,frame=cap.read()
data = pickle.dumps(frame) ### new code
clientsocket.sendall(struct.pack("Q", len(data))+data) ### new code
# sleep(.2)
data = clientsocket.recv(4096*5)
# os.system('clear')
# print (data)
json_data = json.loads(data)
print(json_data["None"])
max_confidence_object = json_data['None'][0][0]
max_confidence_value = float(json_data['None'][0][1])
points = json.loads(data)['points']
if max_confidence_value > .98 and max_confidence_object!='999Non'and max_confidence_object!='000Hand' :
if (max_confidence_object!='999Non'and max_confidence_object!='000Hand' ):
cv2.circle(frame,(points[0],points[1]),25,(0,0,255),-1)
max_conf_object = max_confidence_object
pointsBuffer.append(points[0])
print(max_confidence_object)
print(max_confidence_value)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,max_conf_object[3:],(points[0],points[1]), font, 1,(255,255,255),1,255)
last_time_of_interest = datetime.now()
elif len(pointsBuffer) > 0 and (datetime.now() -last_time_of_interest).total_seconds()>2 :
if (pointsBuffer[0] - pointsBuffer[len(pointsBuffer)-1] ) >0:
print('moving right')
call(['say',max_conf_object[3:]+"moving right"])
postData({"name":max_conf_object,'date_in':datetime.now(),'date_left':computeDateLeft(datetime.now(),max_conf_object[3:],1)})
print("DONE")
else:
print("moving left")
call(['say',max_conf_object[3:]+"moving left"])
pointsBuffer = []
max_conf_object = ""
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('s'):
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment