Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
load_dotenv()
dotenv_file = dotenv.find_dotenv()

logfile = os.getenv("APP_DIR") + '/sleepy_logs.log'
logfile = os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/sleepy_logs.log'
logging.basicConfig(filename=logfile,
filemode='a+',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
Expand All @@ -64,7 +64,7 @@
focus_bounding_box = (None, None, None, None)
classifier_resolution = 256

with open(os.getenv("APP_DIR") + '/user_defined_crop_area.txt', 'r', encoding="utf-8") as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/user_defined_crop_area.txt', 'r', encoding="utf-8") as f:
crop_area = f.read()
focusRegionArr = crop_area.split(',')
print('reading focusRegionArr: ', focusRegionArr)
Expand All @@ -81,7 +81,7 @@
lock_model_use = False
creepy_baby_model = None
try:
with open(os.getenv("APP_DIR") + '/blanket_model/creepy_baby_model.pkl', 'rb') as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/creepy_baby_model.pkl', 'rb') as f:
creepy_baby_model = pickle.load(f)
except Exception as e:
print("No blanket model found at startup: ", e)
Expand Down Expand Up @@ -273,7 +273,7 @@ def process_baby_image_models(self, img, debug_img):
@debounce(180)
def write_wakeness_event(self, wake_status, img):
str_timestamp = str(int(time.time()))
sleep_data_base_path = os.getenv("APP_DIR")
sleep_data_base_path = os.getenv("APP_DIR", "/usr/app/babysleepcoach")
p = sleep_data_base_path + '/' + str_timestamp + '.png'
if wake_status: # woke up
log_string = "1," + str_timestamp + "\n"
Expand All @@ -287,7 +287,7 @@ def write_wakeness_event(self, wake_status, img):
# now = datetime.datetime.now()
# now_time = now.time()
# notifications_enabled = False
# with open(os.getenv("APP_DIR") + '/notifications.txt', 'r', encoding="utf-8") as f:
# with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/notifications.txt', 'r', encoding="utf-8") as f:
# notifications_enabled_text = f.read()
# notifications_enabled = notifications_enabled_text == 'true'
# if notifications_enabled and now_time >= datetime.time(7,00) or now_time <= datetime.time(22,00): # day time
Expand Down Expand Up @@ -460,14 +460,14 @@ def blanket_logic(self, image, body_found, raw_uncropped_image):
proba_string = str(y[0][0]) + ',' + str(y[0][1]) + ',' + str(time.time())
model_proba = proba_string

# with open(os.getenv("APP_DIR") + '/blanket_model/current_output/proba.txt', 'w', encoding="utf-8") as f:
# with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/current_output/proba.txt', 'w', encoding="utf-8") as f:
# f.write(proba_string)
# cv2.imwrite(os.getenv("APP_DIR") + '/blanket_model/current_output/raw.png', image)
# cv2.imwrite(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/current_output/raw.png', image)
except Exception as e:
model_sees_baby = None
print("Something went wrong while invoking creepy blanket model: ", e)

cv2.imwrite(os.getenv("APP_DIR") + '/blanket_model/current_output/raw_uncropped.png', raw_uncropped_image)
cv2.imwrite(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/current_output/raw_uncropped.png', raw_uncropped_image)


def frame_logic(self, raw_img, raw_uncropped_image, debug_frame_q):
Expand Down Expand Up @@ -522,10 +522,10 @@ def frame_logic(self, raw_img, raw_uncropped_image, debug_frame_q):
# This basically does the same thing as the live version, but is very useful for testing
def recorded(self, debug_frame_q, cropped_raw_frame_q):
# cap = cv2.VideoCapture(os.getenv("VIDEO_PATH"))
cap = cv2.VideoCapture(os.getenv("APP_DIR") + '/videos/baby-011013-011040.mp4')
# cap = cv2.VideoCapture(os.getenv("APP_DIR") + '/raw_data/babywaking.mp4')
# cap = cv2.VideoCapture(os.getenv("APP_DIR") + '/raw_data/baby_eyes.mp4')
# cap = cv2.VideoCapture(os.getenv("APP_DIR") + '/raw_data/falling_asleep_eyes.mp4')
cap = cv2.VideoCapture(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/videos/baby-011013-011040.mp4')
# cap = cv2.VideoCapture(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/raw_data/babywaking.mp4')
# cap = cv2.VideoCapture(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/raw_data/baby_eyes.mp4')
# cap = cv2.VideoCapture(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/raw_data/falling_asleep_eyes.mp4')

global focus_bounding_box
global classifier_resolution
Expand Down Expand Up @@ -806,15 +806,15 @@ def getResultAndReasons():
@cross_origin()
def getSleepNotificationsEnabled():
notifications_enabled = None
with open(os.getenv("APP_DIR") + '/notifications.txt', 'r', encoding="utf-8") as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/notifications.txt', 'r', encoding="utf-8") as f:
notifications_enabled = f.read()
return notifications_enabled

@app.route('/setSleepNotificationsEnabled/<enabled>')
@cross_origin()
def setSleepNotificationsEnabled(enabled):
print('enabled? ', enabled)
with open(os.getenv("APP_DIR") + '/notifications.txt', 'w', encoding="utf-8") as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/notifications.txt', 'w', encoding="utf-8") as f:
f.write(enabled)
return 'ok'

Expand Down Expand Up @@ -858,12 +858,12 @@ def setAIFocusRegion(focusRegion):

if focusRegion == 'reset':
print("RESET")
with open(os.getenv("APP_DIR") + '/user_defined_crop_area.txt', 'w', encoding="utf-8") as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/user_defined_crop_area.txt', 'w', encoding="utf-8") as f:
f.write(',,,')
focus_bounding_box = (None, None, None, None)
return 'reset ok'

with open(os.getenv("APP_DIR") + '/user_defined_crop_area.txt', 'w', encoding="utf-8") as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/user_defined_crop_area.txt', 'w', encoding="utf-8") as f:
f.write(focusRegion)

print('focusRegion: ', focusRegion)
Expand All @@ -887,7 +887,7 @@ def setAIFocusRegion(focusRegion):
master_image_data_dict = {"baby": [], "no_baby": []}
# large retrain w/ all the raw images, using newly set bounds
# so user doesnt have to restart/recollect from scratch, if updating
input_location = os.getenv("APP_DIR") + '/blanket_model/input'
input_location = os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/input'
input_paths = os.listdir(input_location)
for path in input_paths:
all_inputs = os.listdir(f"{input_location}/{path}")
Expand All @@ -907,7 +907,7 @@ def setAIFocusRegion(focusRegion):
master_image_data_dict[path].append(cropped_resized_blurred_image_data.flatten().tolist())
cv2.imwrite(f'./tmp/{input}', cropped_resized_blurred_image_data)

with open(os.getenv("APP_DIR") + "/blanket_model/output/image_data.json", "w") as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + "/blanket_model/output/image_data.json", "w") as f:
json.dump(master_image_data_dict, f)

# flatten image data for storage
Expand All @@ -919,10 +919,10 @@ def setAIFocusRegion(focusRegion):
final_clf = svm.SVC(probability=True, C=0.1, gamma=0.0001, kernel='poly')
final_clf.fit(all_images_flat, all_labels)

with open(os.getenv("APP_DIR") + '/blanket_model/creepy_baby_model.pkl','wb') as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/creepy_baby_model.pkl','wb') as f:
pickle.dump(final_clf,f)

with open(os.getenv("APP_DIR") + '/blanket_model/creepy_baby_model.pkl', 'rb') as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/creepy_baby_model.pkl', 'rb') as f:
creepy_baby_model = pickle.load(f)

lock_model_use = False
Expand All @@ -941,25 +941,25 @@ def retrainWithNewSample(classification):
global classifier_resolution
print("\n\n" + classification + "\n\n")

new_input_location = os.getenv("APP_DIR") + '/blanket_model/input/' + classification + '/' + classification + '_' + str(int(time.time())) + '.png'
new_input_location = os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/input/' + classification + '/' + classification + '_' + str(int(time.time())) + '.png'
# move image indicated by user from current_output dir to input dir
shutil.move(os.getenv("APP_DIR") + "/blanket_model/current_output/raw_uncropped.png", new_input_location)
shutil.move(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + "/blanket_model/current_output/raw_uncropped.png", new_input_location)

# read it, move to np
image = Image.open(new_input_location)
image_data = np.asarray(image)

# open & insert into image_data.json, save it off
all_images_dict = {}
with open(os.getenv("APP_DIR") + '/blanket_model/output/image_data.json', "r") as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/output/image_data.json', "r") as f:
all_images_dict = json.load(f)

cropped_image_data = image_data[focus_bounding_box[1]:focus_bounding_box[1]+focus_bounding_box[3], focus_bounding_box[0]:focus_bounding_box[0]+focus_bounding_box[2]]
cropped_resized_image_data, _dim = maintain_aspect_ratio_resize(cropped_image_data, width=classifier_resolution)
cropped_resized_blurred_image_data = cv2.GaussianBlur(cropped_resized_image_data,(3,3),0)

all_images_dict[classification].append(cropped_resized_blurred_image_data.flatten().tolist())
with open(os.getenv("APP_DIR") + "/blanket_model/output/image_data.json", "w") as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + "/blanket_model/output/image_data.json", "w") as f:
json.dump(all_images_dict, f)

# flatten all_fds
Expand All @@ -971,10 +971,10 @@ def retrainWithNewSample(classification):
# final_clf = svm.SVC(probability=True, C=1.0, gamma='auto', kernel='rbf')
final_clf.fit(all_images_flat, all_labels)

with open(os.getenv("APP_DIR") + '/blanket_model/creepy_baby_model.pkl','wb') as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/creepy_baby_model.pkl','wb') as f:
pickle.dump(final_clf,f)

with open(os.getenv("APP_DIR") + '/blanket_model/creepy_baby_model.pkl', 'rb') as f:
with open(os.getenv("APP_DIR", "/usr/app/babysleepcoach") + '/blanket_model/creepy_baby_model.pkl', 'rb') as f:
creepy_baby_model = pickle.load(f)

except Exception as e:
Expand Down Expand Up @@ -1028,4 +1028,4 @@ def receive(producer_q):
# TODO:
# - display cause of speedometer 'ticks' in app somewhere
# - draw more stuff on debug image
# - refactor crazy circles chart to be zoomable and scale
# - refactor crazy circles chart to be zoomable and scale