8 Commits

Author SHA1 Message Date
glmdev
b39236bcb1 remove tensor 2019-02-10 00:19:18 -06:00
glmdev
f7cf37dbf0 new config 2019-02-09 22:54:21 -06:00
glmdev
fb785dab53 generalize backend URL 2019-02-09 17:20:12 -06:00
glmdev
8f3b2e1d0a images 2019-02-09 16:45:18 -06:00
glmdev
ba00272bd3 update 2019-02-09 16:43:19 -06:00
glmdev
f60af7a9cc update 2019-02-09 15:38:10 -06:00
glmdev
2ffafa6719 update 2019-02-09 15:33:44 -06:00
glmdev
31c6293576 .env stuff 2019-02-09 03:50:39 -06:00
25 changed files with 182 additions and 132 deletions

1
.gitignore vendored
View File

@@ -1,6 +1,7 @@
### FIREBASE STUFF ###
hackku-*.json
.env
# Created by https://www.gitignore.io/api/git,python,opencv,pycharm,visualstudio,visualstudiocode
# Edit at https://www.gitignore.io/?templates=git,python,opencv,pycharm,visualstudio,visualstudiocode

161
.idea/workspace.xml generated
View File

@@ -2,22 +2,24 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="f410c5c2-3ffe-4260-93eb-e3d83256d263" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/image_diff_color.py" beforeDir="false" afterPath="$PROJECT_DIR$/image_tools.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/camera.py" beforeDir="false" afterPath="$PROJECT_DIR$/camera.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/capture.jpg" beforeDir="false" afterPath="$PROJECT_DIR$/capture.jpg" afterDir="false" />
<change beforePath="$PROJECT_DIR$/index.py" beforeDir="false" afterPath="$PROJECT_DIR$/index.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ref-img.jpg" beforeDir="false" afterPath="$PROJECT_DIR$/ref-img.jpg" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf>
<file pinned="false" current-in-tab="true">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/index.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="270">
<caret line="18" column="46" selection-start-line="18" selection-start-column="46" selection-end-line="18" selection-end-column="46" />
<state relative-caret-position="450">
<caret line="42" column="46" lean-forward="true" selection-start-line="42" selection-start-column="46" selection-end-line="42" selection-end-column="46" />
<folding>
<element signature="e#0#21#0" expanded="true" />
</folding>
@@ -30,7 +32,7 @@
<provider selected="true" editor-type-id="images" />
</entry>
</file>
<file pinned="false" current-in-tab="false">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/camera.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
@@ -45,8 +47,17 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/.gitignore">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="15">
<caret line="1" column="13" selection-start-line="1" selection-start-column="13" selection-end-line="1" selection-end-column="13" />
<state relative-caret-position="45">
<caret line="3" column="4" selection-start-line="3" selection-start-column="4" selection-end-line="3" selection-end-column="4" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/example.env">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="75">
<caret line="5" column="45" selection-start-line="5" selection-start-column="45" selection-end-line="5" selection-end-column="45" />
</state>
</provider>
</entry>
@@ -54,8 +65,26 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/image_tools.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-379">
<caret line="27" column="1" selection-start-line="27" selection-start-column="1" selection-end-line="27" selection-end-column="1" />
<state relative-caret-position="45">
<caret line="3" column="23" selection-start-line="3" selection-start-column="23" selection-end-line="3" selection-end-column="23" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/.env">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="45">
<caret line="3" column="18" selection-start-line="3" selection-start-column="18" selection-end-line="3" selection-end-column="18" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/config.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="75">
<caret line="5" selection-start-line="5" selection-end-line="5" />
</state>
</provider>
</entry>
@@ -75,10 +104,14 @@
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/.gitignore" />
<option value="$PROJECT_DIR$/image_diff_color.py" />
<option value="$PROJECT_DIR$/camera.py" />
<option value="$PROJECT_DIR$/.gitignore" />
<option value="$PROJECT_DIR$/config.py" />
<option value="$PROJECT_DIR$/image_tools.py" />
<option value="$PROJECT_DIR$/example.env" />
<option value="$PROJECT_DIR$/index.py" />
<option value="$PROJECT_DIR$/.env" />
<option value="$PROJECT_DIR$/camera.py" />
</list>
</option>
</component>
@@ -103,13 +136,7 @@
<path>
<item name="python-backend" type="b2602c69:ProjectViewProjectNode" />
<item name="python-backend" type="462c0819:PsiDirectoryNode" />
<item name="lib" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="python-backend" type="b2602c69:ProjectViewProjectNode" />
<item name="python-backend" type="462c0819:PsiDirectoryNode" />
<item name="lib" type="462c0819:PsiDirectoryNode" />
<item name="test-images" type="462c0819:PsiDirectoryNode" />
<item name="test-images-vert" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
@@ -122,6 +149,11 @@
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
@@ -139,7 +171,7 @@
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="57f4d31d-2726-4888-8d46-d2583bdc9c3d" name="Default Changelist" comment="" />
<changelist id="f410c5c2-3ffe-4260-93eb-e3d83256d263" name="Default Changelist" comment="" />
<created>1549681542752</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
@@ -162,7 +194,7 @@
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Version Control" order="7" />
<window_info anchor="bottom" id="Terminal" order="8" visible="true" weight="0.32972974" />
<window_info anchor="bottom" id="Terminal" order="8" weight="0.32972974" />
<window_info anchor="bottom" id="Python Console" order="9" />
<window_info anchor="bottom" id="Event Log" order="10" side_tool="true" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
@@ -176,19 +208,73 @@
<entry file="file://$PROJECT_DIR$/lib/test-images/lot-empty.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/test.jpg" />
<entry file="file://$PROJECT_DIR$/capture.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/lib/test-images/lot-1-car.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/lib/test-images/lot-2-cars.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/lib/test-images/lot-3-cars.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/test.jpg">
<entry file="file://$PROJECT_DIR$/lib/test-images/lot-4-cars.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/capture.jpg">
<entry file="file://$PROJECT_DIR$/lib/test-images/lot-5-cars-A.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/lib/test-images/lot-5-cars-B.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/image_tools.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="45">
<caret line="3" column="23" selection-start-line="3" selection-start-column="23" selection-end-line="3" selection-end-column="23" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/config.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="75">
<caret line="5" selection-start-line="5" selection-end-line="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/ref-img.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/.gitignore">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="15">
<caret line="1" column="13" selection-start-line="1" selection-start-column="13" selection-end-line="1" selection-end-column="13" />
<state relative-caret-position="45">
<caret line="3" column="4" selection-start-line="3" selection-start-column="4" selection-end-line="3" selection-end-column="4" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/example.env">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="75">
<caret line="5" column="45" selection-start-line="5" selection-start-column="45" selection-end-line="5" selection-end-column="45" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/index.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="450">
<caret line="42" column="46" lean-forward="true" selection-start-line="42" selection-start-column="46" selection-end-line="42" selection-end-column="46" />
<folding>
<element signature="e#0#21#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/.env">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="45">
<caret line="3" column="18" selection-start-line="3" selection-start-column="18" selection-end-line="3" selection-end-column="18" />
</state>
</provider>
</entry>
@@ -202,28 +288,5 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/image_tools.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-379">
<caret line="27" column="1" selection-start-line="27" selection-start-column="1" selection-end-line="27" selection-end-column="1" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/lib/test-images/lot-1-car.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/lib/test-images/lot-5-cars-B.jpg">
<provider selected="true" editor-type-id="images" />
</entry>
<entry file="file://$PROJECT_DIR$/index.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="270">
<caret line="18" column="46" selection-start-line="18" selection-start-column="46" selection-end-line="18" selection-end-column="46" />
<folding>
<element signature="e#0#21#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
</project>

BIN
ai_image.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
ai_image.out.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

View File

@@ -2,13 +2,14 @@ import pygame
import pygame.camera
def capture():
def capture(camera_stream = "/dev/video4"):
pygame.camera.init()
pygame.camera.list_cameras()
cam = pygame.camera.Camera('/dev/video4', (640, 480))
cam = pygame.camera.Camera(camera_stream, (640, 480))
cam.start()
img = cam.get_image()
pygame.image.save(img, './capture.jpg')
cam.stop()
return './capture.jpg'

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 29 KiB

5
config.py Normal file
View File

@@ -0,0 +1,5 @@
from dotenv import load_dotenv
def bootstrap(envfile='./.env'):
load_dotenv(dotenv_path=envfile)

6
example.env Normal file
View File

@@ -0,0 +1,6 @@
POLL_FREQUENCY=15
FIREBASE_CERT_FILE="hackku-272b6e79b2a9.json"
CAMERA_DEVICE="/dev/video0"
LOT_NAME="Lot26"
E_HAT_TRIGGER_LEVEL=4
BACKEND_URL="https://waitnomore.glmdev.tech/"

View File

@@ -1,10 +1,9 @@
from PIL import Image
def average_color(x, y, n, image):
def average_color(x, y, w, h, image):
r, g, b = 0, 0, 0
count = 0
for s in range(x, x + n + 1):
for t in range(y, y + n + 1):
print([x, y, w, h])
for s in range(x, x + w + 1):
for t in range(y, y + h + 1):
pixlr, pixlg, pixlb = image[s, t]
r += pixlr
g += pixlg
@@ -21,64 +20,3 @@ def differences(rgb_tuple1, rgb_tuple2):
def threshold(diff_tuple, trigger=4):
dr, dg, db = diff_tuple
return (((( dr**4 + dg**4 + db**4 ) / 3)**0.25) > trigger) # Thanks to Thomas Atkins, math whiz
#
# reference_image = Image.open('./lib/test-images/lot-empty.jpg').load()
# compare_image = Image.open('./lib/test-images/lot-1-car.jpg').load()
#
# stall_1 = {
# 'location': (54, 79, 109),
# 'reference': average_color(54, 79, 109, reference_image),
# 'compare': average_color(54, 79, 109, compare_image)
# }
#
# stall_2 = {
# 'location': (186, 81, 116),
# 'reference': average_color(186, 81, 116, reference_image),
# 'compare': average_color(186, 81, 116, compare_image)
# }
#
# stall_3 = {
# 'location': (317, 71, 127),
# 'reference': average_color(317, 71, 127, reference_image),
# 'compare': average_color(317, 71, 127, compare_image)
# }
#
# stall_4 = {
# 'location': (469, 76, 100),
# 'reference': average_color(469, 76, 100, reference_image),
# 'compare': average_color(469, 76, 100, compare_image)
# }
#
# stall_5 = {
# 'location': (27, 237, 119),
# 'reference': average_color(27, 237, 119, reference_image),
# 'compare': average_color(27, 237, 119, compare_image)
# }
#
# stall_6 = {
# 'location': (172, 240, 130),
# 'reference': average_color(172, 240, 130, reference_image),
# 'compare': average_color(172, 240, 130, compare_image)
# }
#
# stall_7 = {
# 'location': (317, 234, 144),
# 'reference': average_color(317, 234, 144, reference_image),
# 'compare': average_color(317, 234, 144, compare_image)
# }
#
# stall_8 = {
# 'location': (481, 233, 112),
# 'reference': average_color(481, 233, 112, reference_image),
# 'compare': average_color(481, 233, 112, compare_image)
# }
#
# print(threshold(differences(stall_1['reference'], stall_1['compare'])))
# print(threshold(differences(stall_2['reference'], stall_2['compare'])))
# print(threshold(differences(stall_3['reference'], stall_3['compare'])))
# print(threshold(differences(stall_4['reference'], stall_4['compare'])))
# print(threshold(differences(stall_5['reference'], stall_5['compare'])))
# print(threshold(differences(stall_6['reference'], stall_6['compare'])))
# print(threshold(differences(stall_7['reference'], stall_7['compare'])))
# print(threshold(differences(stall_8['reference'], stall_8['compare'])))

View File

@@ -2,31 +2,53 @@ import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
from PIL import Image
import time
import os
import urllib.request
import camera
import image_tools
import config
config.bootstrap()
# Use a service account
cred = credentials.Certificate('./hackku-272b6e79b2a9.json')
cred = credentials.Certificate(os.getenv('FIREBASE_CERT_FILE'))
firebase_admin.initialize_app(cred)
db = firestore.client()
doc_ref_lot1 = db.collection(u'Lot1')
doc_conf = db.collection('configurations').document(os.getenv('CONFIG_CODE')).get().to_dict()
print(doc_conf)
# Take the picture
# img = Image.open(camera.capture()).load()
img = Image.open('./lib/test-images/lot-3-cars.jpg').load()
reference = Image.open('./lib/test-images/lot-empty.jpg').load()
def poll():
doc_ref_lot = db.collection('lots').document(doc_conf['lot_id'])
stalls = doc_ref_lot1.get()
# Get the reference image
lot_info = doc_ref_lot.collection('info').document('lotInfo').get().to_dict()
urllib.request.urlretrieve(os.getenv('BACKEND_URL')+'img/references/'+lot_info['refImage'], './ref-img.jpg')
reference = Image.open('./ref-img.jpg').load()
for stall in stalls:
#print(u'{} => {}'.format(stall.id, stall.to_dict()))
stall_obj = stall.to_dict()
color_reference = image_tools.average_color(stall_obj['locationX'], stall_obj['locationY'], stall_obj['sideN'], reference)
color_compare = image_tools.average_color(stall_obj['locationX'], stall_obj['locationY'], stall_obj['sideN'], img)
stall_occupied = image_tools.threshold(image_tools.differences(color_reference, color_compare))
stall_obj['open'] = not stall_occupied
# Take the picture
img = Image.open(camera.capture(doc_conf['stream'])).load()
#img = Image.open('./lib/test-images-vert/lot-3-cars.jpg').load()
db.collection(u'Lot1').document(stall.id).set( stall_obj )
doc_ref_stalls = doc_ref_lot.collection('stalls').get()
for stall in doc_ref_stalls:
stall_obj = stall.to_dict()
color_reference = image_tools.average_color(stall_obj['locationX'], stall_obj['locationY'], stall_obj['width'], stall_obj['height'], reference)
color_compare = image_tools.average_color(stall_obj['locationX'], stall_obj['locationY'], stall_obj['width'], stall_obj['height'], img)
stall_occupied = image_tools.threshold(image_tools.differences(color_reference, color_compare), float(doc_conf['e_hat']))
stall_obj['open'] = not stall_occupied
print("Updated: "+stall.id)
doc_ref_lot.collection('stalls').document(stall.id).set(stall_obj)
start_time = time.time()
while True:
poll()
poll_time = float(os.getenv('POLL_FREQUENCY'))
time.sleep(poll_time - ((time.time() - start_time) % poll_time))

Binary file not shown.

Before

Width:  |  Height:  |  Size: 49 KiB

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 60 KiB

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 64 KiB

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 51 KiB

BIN
ref-img.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

14
tensor.py Normal file
View File

@@ -0,0 +1,14 @@
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path, "ai_image.jpg"), output_image_path=os.path.join(execution_path , "ai_image.out.jpg"))
for eachObject in detections:
print(eachObject["name"] , " : " , eachObject["percentage_probability"] )

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB