From 5e18bdbec42169f2796dbb6abf0290653bf79f28 Mon Sep 17 00:00:00 2001 From: Markus Willman <mpewil@utu.fi> Date: Thu, 22 Feb 2018 14:59:32 +0200 Subject: [PATCH] client changes - error logging and exception handling - better bootstrap script using bash --- client/sbs_client.bat | 9 +++-- client/sbs_client.py | 93 ++++++++++++++++++++++++++++++------------- client/sbs_client.sh | 21 ++++++++++ 3 files changed, 93 insertions(+), 30 deletions(-) mode change 100644 => 100755 client/sbs_client.py create mode 100755 client/sbs_client.sh diff --git a/client/sbs_client.bat b/client/sbs_client.bat index 1d4b61f..d38be5b 100644 --- a/client/sbs_client.bat +++ b/client/sbs_client.bat @@ -1,4 +1,7 @@ +@echo off +rem Using the sh variant through msys(git) or similar linux like environment is recommended + :loop - C:\py\sbs\client\sbs_client.py - timeout /t 5 - goto loop \ No newline at end of file + %cd%\sbs_client.py %1 + timeout /t %2 + goto loop diff --git a/client/sbs_client.py b/client/sbs_client.py old mode 100644 new mode 100755 index 58c3ed9..d196fa6 --- a/client/sbs_client.py +++ b/client/sbs_client.py @@ -1,46 +1,85 @@ -# -*- coding: utf-8 -*- +#!/usr/bin/python +# coding=utf-8 """ Created on Tue Feb 20 21:33:57 2018 @author: veskuh """ -import cv2 -import requests +import os, sys, cv2, requests, logging, re from requests_toolbelt.multipart.encoder import MultipartEncoder -import os # Image file name and location -image = ".\images\image.jpg" +TMP_IMAGE = os.path.join(os.path.normpath(os.path.join(os.getcwd(), 'images')), 'image.jpg') +# Server URI +SERVER_URL = "tensorflow.stop.capstone.utu.fi" +# logging.basicConfig(filename = os.path.normpath(os.path.join(os.getcwd(), 'debug.log')), level = logging.DEBUG) +logging.basicConfig(filename = os.path.normpath(os.path.join(os.getcwd(), 'error.log')), level = logging.ERROR) # Sending image to server -def sendImage(): - # Send local image file to tensorflow-client - multipart_data = MultipartEncoder( - fields={ - # a file upload field - "image": ("image.jpg", open(image, "rb"), "image/jpg"), - "stop_code": "Test" - } - ) - - response = requests.post("http://tensorflow.stop.capstone.utu.fi/api/v1/classifyImage/", data=multipart_data, - headers={"Content-Type": multipart_data.content_type}) +def sendImage(stopCode = None): + # Send local image file to tensorflow-client + stopCode = stopCode if stopCode else "unknown" + try: + with open(TMP_IMAGE, "rb") as fp: + multipart_data = MultipartEncoder( + fields = { + "stop_code": stopCode, + "image": (os.path.basename(TMP_IMAGE), fp, "image/jpeg") + } + ) + + response = requests.post("http://" + SERVER_URL + "/api/v1/classifyImage/", + data = multipart_data, + headers = {"Content-Type": multipart_data.content_type} + ) + + if response.status_code != 200: + raise Exception('Service unavailable') + + except Exception as err: + logging.error('An error has occurred whilst sending the file: "{0}", for: {1}'.format(err, stopCode)) # Removing image for security purposes def removeImage(): - os.remove(image) + try: + os.remove(TMP_IMAGE) + except Exception as err: + logging.error('An error has occurred while deleting temporary file: "{0}"'.format(err)) # Taking a picture from default webcam and saving it -def takePicture(): - cap = cv2.VideoCapture(0) - - ret, frame = cap.read() +def takePicture(cameraIndex): + try: + cap = cv2.VideoCapture(cameraIndex) + if not cap.isOpened(): + raise Exception('Camera not available') + + ret, frame = cap.read() + if not ret: + raise Exception('Unable to capture image') + + cv2.imwrite(TMP_IMAGE, frame) + cap.release() + + if not os.path.isfile(TMP_IMAGE): + raise Exception('Saving image to disk failed') + + return True + except Exception as err: + logging.error('An error has occurred while capturing camera image: "{0}"'.format(err)) + return False + +def main(argv): + if argv != None and (len(argv) <= 1 or not re.match("^[\w\d_-]+$", argv[1])): + return False + + if takePicture(0): + sendImage(argv[1]) + removeImage() - cv2.imwrite(image,frame) - cap.release() + return True -takePicture() -sendImage() -removeImage() \ No newline at end of file +if __name__ == "__main__": + if not main(sys.argv): + print("Stop identifier not provided (missing argument)") diff --git a/client/sbs_client.sh b/client/sbs_client.sh new file mode 100755 index 0000000..50941c1 --- /dev/null +++ b/client/sbs_client.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +if [ "$#" -ne 2 ]; then + echo "Usage: $0 stop_code freq_secs" >&2 + exit 1 +fi + +if [ "$2" -lt 3 ]; then + echo "freq_secs must be larger than or equal to 3" >&2 + exit 1 +fi + +while true +do + python "./sbs_client.py" "$1" + ret=$? + if [ $ret -ne 0 ]; then + exit 1 + fi + sleep $2 +done -- GitLab