def delete_temp_file(file):
    try:
        if os.path.exists(file):
            os.remove(file)

    except Exception as e:
        misc.printerr("delete_temp_file", e)
def movefile(rawfile):
    try:
        fp = os.path.split(rawfile)
        newfile = readconfig.DirBackupOriginalImage + "/" + fp[1]
        os.rename(rawfile,newfile)
        print("Move %s to %s" % (rawfile, newfile))

    except Exception as e:
        misc.printerr("movefile", e)
def saveResizedMetadata():
    try:
        if os.path.exists(resizedFile):
            fileSize = os.path.getsize(resizedFile)
            fileTime = datetime.datetime.fromtimestamp(os.path.getctime(resizedFile))
            json_value = "{\"filename\" : \"" + resizedFile + "\", \"filesize\" : \"" + str(fileSize) + "\", \"timestamp\" : \"" + fileTime.strftime("%Y%m%d%H%M%S") +"\"}"
            mysqlDb.mysql_data_input(json_value, 'metadatadocs')
            print("Save metadata of %s" % (resizedFile))
        else:
            print("Cannot find the resized file \"%s\"" % (resizedFile))

    except Exception as e:
        misc.printerr("saveResizedMetadata", e)
def process_generator(cls, method):
    def _method_name(self, event):
        try:
            global rawfile
            rawfile = event.pathname
            ts = datetime.datetime.now()
            tsfmt = ts.strftime("%Y%m%d-%H%M%S.%f")

            if event.mask == pyinotify.IN_CLOSE_WRITE or event.mask == pyinotify.IN_CREATE:
                print("Time : {} | Event : {} | Path name : {}".format(tsfmt, event.maskname, rawfile))
    
            if event.mask == pyinotify.IN_CLOSE_WRITE:
                # Slot in uuid for primary key in MySQL tables
                uuid_value = str(uuid.uuid1())

                # Adding Original file creation timestamp to be added to all file names 
                creationTS = datetime.datetime.fromtimestamp(os.path.getctime(rawfile)) 

                print("uuid: %s" % uuid_value)
                print("timestamp: %s" % str(creationTS))

                # Insert into db
                mysqlDb.mysql_data_input_folderA(uuid_value, rawfile)

                # resize image to 500x500
                process.resizeImage(uuid_value, creationTS, rawfile)

                # save metadata
                # process.saveResizedMetadata()

                # move raw file to folder 0
                process.movefile(rawfile)

                # convert image to Base64 encoding and save it as a json file
                process.convertImage(uuid_value, creationTS)

                # closing
                print("")
                print("")

        except Exception as e:
            misc.printerr("_method_name", e)

    try:
        _method_name.__name__ = "process_{}".format(method)
        setattr(cls, _method_name.__name__, _method_name)

    except Exception as e:
        misc.printerr("process_generator", e)
def convertImage(uuid_value, creationTS):
    try:
        import base64
        import substring
        import shutil
        import mysqlx
        import requests

        with open(resizedFile, "rb") as image_file:
            encoded_string = base64.b64encode(image_file.read()).decode('utf-8')

        # json_value =" { \"instances\" : [ { \"image_bytes\" : { \"b64\" :\"" + encoded_string + "\" }, \"key\" :\"" + readconfig.ImageKey + "\" } ] }" 
        json_value =' { "instances" : [ { "image_bytes" : { "b64" :"' + encoded_string + '" }, "key" : "' + readconfig.ImageKey + '" } ] }'

        json_file_name = readconfig.CCTVName + "-" + creationTS.strftime("%Y%m%d%H%M%S") + ".json"
        global jsonBase64Path
        jsonBase64Path = readconfig.DirBase64JSON + "/" + json_file_name 
        with open(jsonBase64Path, 'w') as outfile:
            # json.dump(json_value, outfile)
            outfile.write(json_value) 
            outfile.close()

        # Insert into db
        mysqlDb.mysql_data_input_folderC(uuid_value, jsonBase64Path)

        print("Convert image of %s to %s" % (resizedFile, jsonBase64Path))

        # Send to tensorflow and save it to json file
        url = 'http://localhost:{}/v1/models/default:predict'.format(readconfig.TensorflowPort)

        response = requests.post(url, data = json_value)
        json_pred_file_name = readconfig.CCTVName + "-" + creationTS.strftime("%Y%m%d%H%M%S") + ".json"
        jsonPredPath = readconfig.DirPredictionJSON + "/" + json_pred_file_name
        json_pred = str(response.json())
        json_string = json_pred.replace("'","\"")
        print(json_string)
#        json_pred = "{}"
        with open(jsonPredPath, 'w') as outfile:
            outfile.write(json_string)
            outfile.close()

        # Insert into db
        mysqlDb.mysql_data_input_folderD(uuid_value, jsonPredPath,json_string)

        #print(response.json()) 
        print("Save response from TensorFlow as %s" % jsonPredPath)

    except Exception as e:
        misc.printerr("convertImage", e)
def resizeImage(uuid_value, creationTS, rawfile):
    import sys
    from resizeimage import resizeimage 
    from PIL import Image

    try:
        with open(rawfile, 'rb') as f:
            with Image.open(f) as image:
                global resizedFile
                resizedFile = "%s/%s-%s.jpg" % (readconfig.DirCheckedImage, readconfig.CCTVName, creationTS.strftime("%Y%m%d%H%M%S"))
                cover = resizeimage.resize_thumbnail(image, [500, 500])
                cover.save(resizedFile, image.format)

                # Insert into db
                mysqlDb.mysql_data_input_folderB(uuid_value, resizedFile)

                print("Resize image of %s to %s" % (rawfile, resizedFile))

    except Exception as e:
        print(e)
        misc.printerr("resizeImage", e)
Пример #7
0
            ServerUsername = line.split("=")[1].strip()
        if line.startswith("ServerPassword="******"=")[1].strip()
        if line.startswith("DirOriginalImage="):
            DirOriginalImage = line.split("=")[1].strip()
        if line.startswith("DirCheckedImage="):
            DirCheckedImage = line.split("=")[1].strip()
        if line.startswith("DirBase64JSON="):
            DirBase64JSON = line.split("=")[1].strip()
        if line.startswith("DirPredictionJSON="):
            DirPredictionJSON = line.split("=")[1].strip()
        if line.startswith("DirBackupOriginalImage="):
            DirBackupOriginalImage = line.split("=")[1].strip()
        if line.startswith("DBServerIPAddress="):
            DBServerIPAddress = line.split("=")[1].strip()
        if line.startswith("DBUsername="******"=")[1].strip()
        if line.startswith("DBPassword="******"=")[1].strip()
        if line.startswith("DatabaseName="):
            DatabaseName = line.split("=")[1].strip()
        if line.startswith("TensorflowPort="):
            TensorflowPort = line.split("=")[1].strip()
        if line.startswith("CCTVName="):
            CCTVName = line.split("=")[1].strip()
        if line.startswith("ImageKey="):
            ImageKey = line.split("=")[1].strip()

except Exception as e:
    misc.printerr("readconfig", e)
    except Exception as e:
        misc.printerr("delete_temp_file", e)




try:

    # remove pid and log files
#    delete_temp_file("/tmp/mydaemon.pid")
    delete_temp_file("/tmp/error.log")
    delete_temp_file("/tmp/out.log")

    for method in EventProcessor._methods:
        process_generator(EventProcessor, method)

    watch_manager = pyinotify.WatchManager()
    event_notifier = pyinotify.Notifier(watch_manager, EventProcessor())
    watch_this = os.path.abspath(readconfig.DirOriginalImage)

    watch_manager.add_watch(watch_this, pyinotify.IN_CREATE
                            | pyinotify.IN_ACCESS | pyinotify.IN_ATTRIB | pyinotify.IN_CLOSE_NOWRITE | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE
                            | pyinotify.IN_DELETE_SELF | pyinotify.IN_IGNORED | pyinotify.IN_MODIFY | pyinotify.IN_MOVE_SELF | pyinotify.IN_MOVED_FROM
                            | pyinotify.IN_MOVED_TO | pyinotify.IN_Q_OVERFLOW | pyinotify.IN_UNMOUNT)

    event_notifier.loop(daemonize=True, pid_file='/tmp/mydaemon.pid', stdout='/tmp/out.log', stderr='/tmp/error.log')

except Exception as e:

    misc.printerr("main", e)