Пример #1
0
def sleep_to_evening():
	print("calculating sleep time..")
	task_year	= (datetime.datetime.now() + datetime.timedelta(days=1)).year		
	task_month	= (datetime.datetime.now() + datetime.timedelta(days=1)).month
	task_day	= datetime.datetime.now().day
	task_hour	= 21
	task_minute	= 0
	sleeptime	= datetime.datetime(task_year, task_month, task_day, task_hour, task_minute)-datetime.datetime.now()
	print("Sleeping",sleeptime.seconds/60/60," hours")
	send_to_telegram(chat,"Sleeping "+str(sleeptime.seconds/60/60)+" hours")
	time.sleep(sleeptime.seconds)
	print("job started..")
	send_to_telegram(chat,"job started..")
def separate():
    gpu_count = 1

    chat = PASTE_GROUP_ID

    screenshots_path = "/home/alex/storage/shop_screens/video/"
    processed_path = "/home/alex/storage/shop_screens/video/processed/"
    shop_names = ["Altuf", "Avangard", "Mar", "Tag"]

    if host_check("scriptlab.net"):
        print("scriptlab.net - Ok")
    else:
        print("scriptlab.net - Unavailable. Exit")
        exit()

    if host_check("10.2.4.95"):
        print("10.2.4.95 (images server) - ok")
    else:
        print("10.2.4.95 (images server) - Unavailable. Exit")
        send_to_telegram(
            chat,
            "10.2.4.95 (images server) - Unavailable. Unable to terminate records. Exit"
        )
        exit()

    if host_check("10.2.4.25"):
        print("10.2.4.25 (SQL) - ok")
    else:
        print("10.2.4.25 (SQL) - Unavailable. Exit")
        send_to_telegram(
            chat,
            "10.2.4.25 (SQL) - Unavailable. Unable to terminate records. Exit")
        exit()

    files_total_count = 0
    print("files count..")
    #send_to_telegram(chat,"files dividing started..")
    for shop_id in range(4):
        files = os.listdir(screenshots_path + shop_names[shop_id] + "/grabs/")
        files = natsorted(files, alg=ns.PATH)
        files_total_count += len(files)
    print(files_total_count)
    #send_to_telegram(chat,str(files_total_count)+" in queue")

    files_count = 0
    gpu_id = 0

    conn = pymssql.connect(server='10.2.4.25',
                           user='******',
                           password=PASTE_PASS,
                           database='shopEvents')
    cursor = conn.cursor()

    # reset all to Not processed state
    query = "delete from files_to_process"
    cursor.execute(query)
    conn.commit()

    for shop_id in range(4):
        print(shop_names[shop_id])
        files = os.listdir(screenshots_path + shop_names[shop_id] + "/grabs/")
        files = natsorted(files, alg=ns.PATH)
        bar = progressbar.ProgressBar(maxval=len(files),
                                      widgets=[
                                          progressbar.Bar('=', '[', ']'), ' ',
                                          progressbar.Percentage()
                                      ])
        bar.start()
        for file_id in range(len(files)):

            bar.update(file_id + 1)
            file_full_path = screenshots_path + shop_names[
                shop_id] + "/grabs/" + files[file_id]
            if os.stat(file_full_path).st_size == 0:  #empty
                os.remove(file_full_path)
                continue
            if file_full_path.find(".jpg") == -1:  #not jpg
                continue

            query = "INSERT INTO files_to_process (file_id,file_full_path,shop_id,gpu_id,file_name) VALUES (" + str(
                files_count) + ",'" + file_full_path + "'," + str(
                    shop_id) + "," + str(gpu_id) + ",'" + files[file_id] + "')"
            cursor.execute(query)
            conn.commit()

            if (gpu_count > 1):
                if files_count > files_total_count / (
                        gpu_count) and gpu_id < gpu_count:
                    print(file_full_path, shop_id, gpu_id)
                    gpu_id += 1
                    files_count = 0
                else:
                    files_count += 1
            else:
                files_count += 1
        bar.update(len(files))

    print("\nprepared:", files_count, "files")
def terminator():
    life_day_lenght = 60
    chat = "-1001448066127"

    if host_check("scriptlab.net"):
        print("scriptlab.net - Ok")
    else:
        print("scriptlab.net - Unavailable. Exit")
        exit()

    if host_check("10.2.4.95"):
        print("10.2.4.95 (images server) - ok")
    else:
        print("10.2.4.95 (images server) - Unavailable. Exit")
        send_to_telegram(
            chat,
            "10.2.4.95 (images server) - Unavailable. Unable to terminate records. Exit"
        )
        exit()

    if host_check("10.2.4.25"):
        print("10.2.4.25 (SQL) - ok")
    else:
        print("10.2.4.25 (SQL) - Unavailable. Exit")
        send_to_telegram(
            chat,
            "10.2.4.25 (SQL) - Unavailable. Unable to terminate records. Exit")
        exit()

    date_current = datetime.now()

    #send_to_telegram(chat,"Removing old files..")

    conn = pymssql.connect(server='10.2.4.25',
                           user='******',
                           password='******',
                           database='shopEvents')
    cursor = conn.cursor()
    time_limit = (
        date_current -
        dt.timedelta(days=life_day_lenght)).strftime('%Y-%m-%d %H:%M:%S')

    # === Remove files with SQL
    query = "SELECT path FROM files where date<'" + time_limit + "'"
    cursor.execute(query)
    answer = cursor.fetchall()
    count_of_files_to_delete = len(answer)
    log_message = "SQL: Removing " + str(count_of_files_to_delete) + " files"
    print(log_message)
    send_to_telegram(chat, log_message)
    bar = progressbar.ProgressBar(maxval=count_of_files_to_delete,
                                  widgets=[
                                      progressbar.Bar('=', '[', ']'), ' ',
                                      progressbar.Percentage()
                                  ])
    i = 0
    bar.start()
    file_not_found_count = 0
    file_removed_count = 0
    for file_name_to_delete in answer:
        if os.path.isfile(file_name_to_delete[0]):
            os.remove(file_name_to_delete[0])
            file_removed_count = file_removed_count + 1
        else:
            file_not_found_count = file_not_found_count + 1
        bar.update(i + 1)
        i = i + 1
    bar.finish()
    log_message = "Removed: " + str(file_removed_count) + " files"
    print(log_message)
    if file_removed_count:
        send_to_telegram(chat, log_message)

    if file_not_found_count:
        log_message = "Not found " + str(file_not_found_count) + " files"
        print(log_message)
        send_to_telegram(chat, log_message)

    # === Remove SQL reocords: files
    print("Removing SQL records: files")
    query = "DELETE FROM files where date<'" + time_limit + "'"
    cursor.execute(query)
    conn.commit()

    # === Remove SQL records: events
    print("Removing SQL records: events")
    query = "DELETE FROM events WHERE eventDate<'" + time_limit + "'"
    cursor.execute(query)
    conn.commit()

    # === File system: remove files
    #print("File system: Removing old files")
    file_date = filedate()
    files_removed_count = 0
    shares_path = "/mnt/shares/"
    for root, subdirs, files in os.walk(shares_path):
        list_file_path = os.path.join(root, 'my-directory-list.txt')
        with open(list_file_path, 'wb') as list_file:
            log_message = "Processing " + str(
                len(files)) + " files in directory: " + list_file_path
            print(log_message)
            #send_to_telegram(chat,log_message)
            bar = progressbar.ProgressBar(maxval=len(files),
                                          widgets=[
                                              progressbar.Bar('=', '[', ']'),
                                              ' ',
                                              progressbar.Percentage()
                                          ])
            i = 0
            bar.start()
            for filename in files:

                if ".jpg" in filename:
                    #print(filename)
                    file_date.update(filename)
                    file_path = os.path.join(root, filename)

                    #time_difference=(date_current-datetime.fromtimestamp( os.path.getctime(file_path) )).total_seconds()
                    #day_difference=int(time_difference/60/60/24)
                    time_difference = date_current - file_date.dateFormat()
                    day_difference = int(time_difference.total_seconds() / 60 /
                                         60 / 24)

                    if day_difference > life_day_lenght:
                        os.remove(file_path)
                        files_removed_count += 1
                    #else:
                    #	print("New file "+str(time_difference)+": ("+str(day_difference)+"<="+str(life_day_lenght)+"): "+file_path)
                bar.update(i + 1)
                i = i + 1
            bar.finish()
            log_message = "Removed by file system: " + str(
                files_removed_count) + " files"
            print(log_message)
            #send_to_telegram(chat,log_message)
            files_removed_count = 0
    log_message = "Terminator job complete. normal exit"
    print(log_message)
    send_to_telegram(chat, log_message)
Пример #4
0
def main(argv):

    ops_shop_id = ""
    ops_gpu = 0

    try:
        opts, args = getopt.getopt(
            argv, "hd:g:s:f:t",
            ["interval=", "gpu=", "shop_id=", "from=", "to="])
    except getopt.GetoptError:
        print('detection_9_off.py -d 2019-05-22 -g 0 -s 0 -f 0 -t 100')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print('detection_9_off.py -d 2019-05-22 -g 0 -s 0 -f 0 -t 100')
            sys.exit()
        elif opt in ("-d", "--date"):
            ops_date = arg
        elif opt in ("-g", "--gpu"):
            ops_gpu = int(arg)
        elif opt in ("-s", "--shop_id"):
            ops_shop_id = int(arg)
        elif opt in ("-f", "--from"):
            ops_from = int(arg)
        elif opt in ("-t", "--to"):
            ops_from = int(arg)

    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # see issue #152
    os.environ["CUDA_VISIBLE_DEVICES"] = str(ops_gpu)

    screenshots_path = "/home/alex/storage/shop_screens/video/"
    processed_path = "/home/alex/storage/shop_screens/video/processed/"
    save_images_path = "/home/alex/storage/rcimages/"
    shop_names = [
        "Altuf", "Avangard", "Mar", "Tag", "Sklad", "SkladSM1", "SkladSM2"
    ]
    NUM_CLASSES = 18
    PATH_TO_CKPT = '../../inference_v7_12097/frozen_inference_graph.pb'
    PATH_TO_LABELS = '../training/mscoco_label_map.pbtxt'

    score_limit = 0.7
    sizeMin = 10
    sizeMax = 1090
    font = cv2.FONT_HERSHEY_SIMPLEX

    #sleep_to_evening()
    prepare_data()

    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    file_date = filedate()
    with open('sql.pass', 'r') as sql_pass_file:
        sql_pass = sql_pass_file.read()

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:

            conn = pymssql.connect(server='10.2.4.124',
                                   user='******',
                                   password=sql_pass,
                                   database='shopEvents')
            cursor = conn.cursor()

            while (True):

                detectedImagesCount = 0

                query = "SELECT file_full_path,shop_id,file_name FROM files_to_process where gpu_id = " + str(
                    ops_gpu) + " ORDER BY file_id"
                cursor.execute(query)
                sql_answer = cursor.fetchall()

                send_to_telegram(
                    chat,
                    "detection " + str(len(sql_answer)) + " files started")
                print("len(sql_answer)", len(sql_answer))
                for record_current in sql_answer:

                    file_full_path = record_current[0]
                    shop_id = record_current[1]
                    file_name_sql = record_current[2]

                    file_sql_query = ""

                    if not os.path.isfile(file_full_path):
                        continue

                    file_date.update(file_name_sql)

                    image_np = cv2.imread(file_full_path)
                    image_original = image_np.copy()

                    #RECOGNIZE++
                    image_np_expanded = np.expand_dims(image_np, axis=0)
                    image_tensor = detection_graph.get_tensor_by_name(
                        'image_tensor:0')
                    boxes = detection_graph.get_tensor_by_name(
                        'detection_boxes:0')
                    scores = detection_graph.get_tensor_by_name(
                        'detection_scores:0')
                    classes = detection_graph.get_tensor_by_name(
                        'detection_classes:0')
                    num_detections = detection_graph.get_tensor_by_name(
                        'num_detections:0')
                    (boxes, scores, classes, num_detections) = sess.run(
                        [boxes, scores, classes, num_detections],
                        feed_dict={image_tensor: image_np_expanded})
                    final_score = np.squeeze(scores)

                    #get objects detected count
                    objectsDetectedCount = 0
                    score_summ = 0
                    npsboxes = np.squeeze(boxes)
                    imageheight, imagewidth = image_np.shape[:2]

                    for i in range(100):

                        current_class_id = np.squeeze(classes).astype(
                            np.int32)[i]
                        current_class_name = category_index[current_class_id][
                            'name']

                        if (scores is None or final_score[i] > score_limit
                            ) and "banknotes" in current_class_name:

                            rx1 = int(npsboxes[i][1] * imagewidth)  #1 xLeft
                            ry1 = int(npsboxes[i][0] * imageheight)  #0 yTop
                            rx2 = int(npsboxes[i][3] * imagewidth)  #3 xRight
                            ry2 = int(npsboxes[i][2] * imageheight)  #2 yBottom

                            xlen = rx2 - rx1
                            ylen = ry2 - ry1

                            if xlen > sizeMin and xlen < sizeMax and ylen > sizeMin and ylen < sizeMax:

                                file_name = file_name_sql.replace(".jpg", "")

                                file_name_all_boxes = file_name + "_all_boxes.jpg"
                                save_path_all_boxes = save_images_path + str(
                                    shop_id
                                ) + "/original/" + file_name_all_boxes
                                file_name_original = file_name + ".jpg"
                                save_path_original = save_images_path + str(
                                    shop_id
                                ) + "/original/" + file_name_original
                                file_name_box = file_name + "_" + str(
                                    objectsDetectedCount) + ".jpg"
                                save_path_box = save_images_path + str(
                                    shop_id) + "/boxed/" + file_name_box

                                file_sql_query = file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('" + save_path_box + "','" + file_date.sqlFormat(
                                ) + "'," + str(shop_id) + ");"
                                query = "INSERT INTO events (eventDate,objectsCount,middleScore,FileName,shop_id,box_id,box_left,box_right,box_top,box_bottom,filename_original,filename_box,file_source_path) VALUES ('" + file_date.sqlFormat(
                                ) + "',1," + str(
                                    final_score[i]
                                ) + ",'" + file_name_all_boxes + "'," + str(
                                    shop_id
                                ) + "," + str(
                                    objectsDetectedCount
                                ) + "," + str(rx1) + "," + str(
                                    rx2
                                ) + "," + str(ry1) + "," + str(
                                    ry2
                                ) + ",'" + file_name_original + "','" + file_name_box + "','" + file_full_path + "')"
                                cursor.execute(query)
                                conn.commit()
                                score_summ = score_summ + final_score[i]
                                image_np_current_box = image_original.copy()

                                object_description = str(
                                    round(final_score[i] * 100)) + "%"
                                fontScale = 0.5
                                lineType = 2

                                #boxes
                                cv2.rectangle(image_np_current_box, (rx1, ry1),
                                              (rx2, ry2), (255, 0, 0),
                                              2)  #regular box
                                cv2.rectangle(image_np_current_box,
                                              (rx1, ry1 + 1),
                                              (rx1 + 200, ry1 - 15),
                                              (0, 255, 0),
                                              -1)  #text background
                                cv2.rectangle(image_np, (rx1, ry1), (rx2, ry2),
                                              (0, 255, 0), 2)  #regular box
                                cv2.rectangle(image_np, (rx1, ry1 + 1),
                                              (rx1 + 200, ry1 - 15),
                                              (0, 255, 0),
                                              -1)  #text background

                                cv2.putText(image_np_current_box,
                                            object_description, (rx1, ry1),
                                            cv2.FONT_HERSHEY_SIMPLEX,
                                            fontScale, (0, 0, 0), lineType)
                                cv2.putText(image_np, object_description,
                                            (rx1, ry1),
                                            cv2.FONT_HERSHEY_SIMPLEX,
                                            fontScale, (0, 0, 0), lineType)

                                #save ONE BOX
                                cv2.imwrite(save_path_box,
                                            image_np_current_box)
                                print("cv2.imwrite image_np_current_box:",
                                      save_path_box)
                                objectsDetectedCount += 1

                    if objectsDetectedCount > 0:
                        score_summString = '%.2f' % (
                            score_summ / objectsDetectedCount)  #middle
                        score_summStringH = '%.2f' % (
                            score_summ / objectsDetectedCount * 100)  #middle
                    else:
                        score_summString = "0"
                        score_summStringH = "0"

                    cv2.putText(
                        image_np,
                        str(objectsDetectedCount) + ":" + score_summStringH +
                        "%", (10, 150), font, 1, (255, 255, 255), 1,
                        cv2.LINE_AA)
                    #RECOGNIZE--

                    if objectsDetectedCount > 0:
                        #save ALL BOXES
                        cv2.imwrite(save_path_all_boxes, image_np)
                        print("cv2.imwrite image_np:", save_path_all_boxes)
                        detectedImagesCount += 1

                        file_sql_query = file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('" + save_path_all_boxes + "','" + file_date.sqlFormat(
                        ) + "'," + str(shop_id) + ");"

                    file_full_path_new = processed_path + shop_names[
                        shop_id] + "_" + file_name_sql
                    os.rename(file_full_path, file_full_path_new)
                    print("os.rename From:", file_full_path, "To:",
                          file_full_path_new)

                    # == Save filename to sql
                    file_sql_query = file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('" + file_full_path_new + "','" + file_date.sqlFormat(
                    ) + "'," + str(shop_id) + ");"
                    cursor.execute(file_sql_query)
                    conn.commit()

                    #remove record from file_to_process
                    file_to_process_query = "DELETE FROM files_to_process where file_full_path='" + file_full_path + "'"
                    cursor.execute(file_to_process_query)
                    conn.commit()

                sleep_to_evening()
                prepare_data()
Пример #5
0
#chat = "-1001448066127"
chat = "-227727734"

if host_check("scriptlab.net"):
    print("scriptlab.net - Ok")
else:
    print("scriptlab.net - Unavailable. Exit")
    exit()

if host_check("10.2.4.95"):
    print("10.2.4.95 (images server) - ok")
else:
    print("10.2.4.95 (images server) - Unavailable. Exit")
    send_to_telegram(
        chat,
        "10.2.4.95 (images server) - Unavailable. Unable to add records. Exit")
    exit()

if host_check("10.2.4.124"):
    print("10.2.4.124 (SQL) - ok")
else:
    print("10.2.4.124 (SQL) - Unavailable. Exit")
    send_to_telegram(
        chat, "10.2.4.124 (SQL) - Unavailable. Unable to add records. Exit")
    exit()


def load_image_into_numpy_array(image):
    (im_width, im_height) = image.size
    return np.array(image.getdata()).reshape(
def main(argv):

    ops_shop_id = ""
    ops_gpu = 0
    #ops_date=""

    try:
        opts, args = getopt.getopt(argv, "hd:g:s:",
                                   ["interval=", "gpu=", "shop_id="])
    except getopt.GetoptError:
        print('detection_6_off.py -d 2019-05-22 -g 0 -s 0')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print('detection_6_off.py -d 2019-05-22 -g 0 -s 0')
            sys.exit()
        elif opt in ("-d", "--date"):
            ops_date = arg
        elif opt in ("-g", "--gpu"):
            ops_gpu = int(arg)
        elif opt in ("-s", "--shop_id"):
            ops_shop_id = int(arg)
    #print ('Interval: ', ops_interval)
    print('Gpu: ', ops_gpu)

    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  # see issue #152
    os.environ["CUDA_VISIBLE_DEVICES"] = str(ops_gpu)

    screenshots_path = "/mnt/shares/shop_screens/"
    processed_path = "/mnt/shares/shop_screens/processed/"
    shop_names = ["Altuf", "Avangard", "Mar", "Tag"]
    #NUM_CLASSES = 1
    NUM_CLASSES = 18
    #PATH_TO_CKPT = '../../banknotes_inference_graph_v3_20904/frozen_inference_graph.pb'
    PATH_TO_CKPT = '../../inference_v6_10591/frozen_inference_graph.pb'
    #PATH_TO_LABELS = '../training/object-detection.pbtxt'
    PATH_TO_LABELS = '../training/mscoco_label_map.pbtxt'
    save_images_path = "/mnt/shares/recognized_images/"
    score_limit = 0.1
    sizeMin = 10
    sizeMax = 1090
    font = cv2.FONT_HERSHEY_SIMPLEX

    conn = pymssql.connect(server='10.2.4.25',
                           user='******',
                           password='******',
                           database='shopEvents')
    cursor = conn.cursor()

    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')

    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(
        label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    category_index = label_map_util.create_category_index(categories)

    file_date = filedate()

    # ==== Temporary code
    # query	= "SELECT path FROM files where path<>'' ORDER BY path"
    # cursor.execute(query)
    # sql_answer=cursor.fetchall()
    # # == prepare array
    # files_from_sql=[]
    # for record_current in sql_answer:
    # files_from_sql.append(record_current[0])
    # print("Recognized files array size: "+str(len(files_from_sql)))

    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:

            #step in every shop
            for shop_id in range(4):
                if ops_shop_id != "" and shop_id != int(ops_shop_id):
                    continue
                #dtnow=datetime.datetime.now()
                #today=dtnow.strftime("%Y-%m-%d_08-56")
                files = os.listdir(screenshots_path + shop_names[shop_id] +
                                   "/grabs/")
                #images = list(filter(lambda x: today in x, files))

                if len(files) > 0:
                    send_to_telegram(
                        chat,
                        str(len(files)) + " " + shop_names[shop_id] + " begin")

                detectedImagesCount = 0

                bar = progressbar.ProgressBar(maxval=len(files),
                                              widgets=[
                                                  progressbar.Bar(
                                                      '=', '[', ']'), ' ',
                                                  progressbar.Percentage()
                                              ])
                bar.start()
                #files=files.sort()
                files = natsorted(files, alg=ns.PATH)
                print("Processing " + str(len(files)) + " files of " +
                      shop_names[shop_id])
                for file_id in range(len(files)):

                    bar.update(file_id + 1)

                    file_sql_query = ""

                    file_full_path = screenshots_path + shop_names[
                        shop_id] + "/grabs/" + files[file_id]

                    if os.stat(file_full_path).st_size == 0:
                        #drop file
                        os.remove(file_full_path)
                        #print(files[file_id],"removed as empty")
                        continue
                    if file_full_path.find(".jpg") == -1:
                        #print(files[file_id],"skipped as not jpg file")
                        continue

                    file_date.update(files[file_id])

                    # Temporary code
                    # if file_full_path in files_from_sql:
                    # file_full_path_new	= processed_path+shop_names[shop_id]+"_"+files[file_id]
                    # os.rename(file_full_path,file_full_path_new)
                    # move_sql_query = "INSERT INTO files (path,date,shop_id) VALUES ('"+file_full_path_new+"','"+file_date.sqlFormat()+"',"+str(shop_id)+");"
                    # cursor.execute(move_sql_query)
                    # conn.commit()
                    # continue

                    # ============ print(shop_id,"recognizing:",files[file_id],file_id,"of",len(files))

                    #print(file_date.sqlFormat())
                    #print("y",file_date.year)
                    #print("date extracted from filename")
                    #exit()

                    image_np = cv2.imread(file_full_path)
                    #image_original	= image_np
                    image_original = image_np.copy()
                    #image_original	= copy.deepcopy(image_np)

                    #RECOGNIZE++
                    image_np_expanded = np.expand_dims(image_np, axis=0)
                    image_tensor = detection_graph.get_tensor_by_name(
                        'image_tensor:0')
                    boxes = detection_graph.get_tensor_by_name(
                        'detection_boxes:0')
                    scores = detection_graph.get_tensor_by_name(
                        'detection_scores:0')
                    classes = detection_graph.get_tensor_by_name(
                        'detection_classes:0')
                    num_detections = detection_graph.get_tensor_by_name(
                        'num_detections:0')
                    (boxes, scores, classes, num_detections) = sess.run(
                        [boxes, scores, classes, num_detections],
                        feed_dict={image_tensor: image_np_expanded})
                    final_score = np.squeeze(scores)

                    #get objects detected count
                    objectsDetectedCount = 0
                    score_summ = 0
                    npsboxes = np.squeeze(boxes)
                    imageheight, imagewidth = image_np.shape[:2]

                    for i in range(100):

                        current_class_id = np.squeeze(classes).astype(
                            np.int32)[i]
                        current_class_name = category_index[current_class_id][
                            'name']

                        if (scores is None or final_score[i] > score_limit
                            ) and "banknotes" in current_class_name:

                            rx1 = int(npsboxes[i][1] * imagewidth)  #1 xLeft
                            ry1 = int(npsboxes[i][0] * imageheight)  #0 yTop
                            rx2 = int(npsboxes[i][3] * imagewidth)  #3 xRight
                            ry2 = int(npsboxes[i][2] * imageheight)  #2 yBottom

                            xlen = rx2 - rx1
                            ylen = ry2 - ry1

                            if xlen > sizeMin and xlen < sizeMax and ylen > sizeMin and ylen < sizeMax:

                                file_name = files[file_id].replace(".jpg", "")
                                file_name_all_boxes = file_name + "_all_boxes.jpg"
                                save_path_all_boxes = save_images_path + str(
                                    shop_id
                                ) + "/original/" + file_name_all_boxes
                                file_name_original = file_name + ".jpg"
                                save_path_original = save_images_path + str(
                                    shop_id
                                ) + "/original/" + file_name_original
                                file_name_box = file_name + "_" + str(
                                    objectsDetectedCount) + ".jpg"
                                save_path_box = save_images_path + str(
                                    shop_id) + "/boxed/" + file_name_box
                                file_sql_query = file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('" + save_path_box + "','" + file_date.sqlFormat(
                                ) + "'," + str(shop_id) + ");"
                                query = "INSERT INTO events (eventDate,objectsCount,middleScore,FileName,shop_id,box_id,box_left,box_right,box_top,box_bottom,filename_original,filename_box,file_source_path) VALUES ('" + file_date.sqlFormat(
                                ) + "',1," + str(
                                    final_score[i]
                                ) + ",'" + file_name_all_boxes + "'," + str(
                                    shop_id
                                ) + "," + str(
                                    objectsDetectedCount
                                ) + "," + str(rx1) + "," + str(
                                    rx2
                                ) + "," + str(ry1) + "," + str(
                                    ry2
                                ) + ",'" + file_name_original + "','" + file_name_box + "','" + file_full_path + "')"
                                cursor.execute(query)
                                conn.commit()
                                score_summ = score_summ + final_score[i]
                                image_np_current_box = image_original.copy()

                                object_description = str(
                                    round(final_score[i] *
                                          100)) + "% " + current_class_name
                                fontScale = 0.5
                                lineType = 2

                                #boxes
                                cv2.rectangle(image_np_current_box, (rx1, ry1),
                                              (rx2, ry2), (255, 0, 0),
                                              2)  #regular box
                                cv2.rectangle(image_np_current_box,
                                              (rx1, ry1 + 1),
                                              (rx1 + 200, ry1 - 15),
                                              (0, 255, 0),
                                              -1)  #text background
                                cv2.rectangle(image_np, (rx1, ry1), (rx2, ry2),
                                              (0, 255, 0), 2)  #regular box
                                cv2.rectangle(image_np, (rx1, ry1 + 1),
                                              (rx1 + 200, ry1 - 15),
                                              (0, 255, 0),
                                              -1)  #text background

                                cv2.putText(image_np_current_box,
                                            object_description, (rx1, ry1),
                                            cv2.FONT_HERSHEY_SIMPLEX,
                                            fontScale, (0, 0, 0), lineType)
                                cv2.putText(image_np, object_description,
                                            (rx1, ry1),
                                            cv2.FONT_HERSHEY_SIMPLEX,
                                            fontScale, (0, 0, 0), lineType)

                                #save ONE BOX
                                cv2.imwrite(save_path_box,
                                            image_np_current_box)
                                objectsDetectedCount += 1

                    if objectsDetectedCount > 0:
                        score_summString = '%.2f' % (
                            score_summ / objectsDetectedCount)  #middle
                        score_summStringH = '%.2f' % (
                            score_summ / objectsDetectedCount * 100)  #middle
                    else:
                        score_summString = "0"
                        score_summStringH = "0"

                    cv2.putText(
                        image_np,
                        str(objectsDetectedCount) + ":" + score_summStringH +
                        "%", (10, 150), font, 1, (255, 255, 255), 1,
                        cv2.LINE_AA)
                    #RECOGNIZE--

                    if objectsDetectedCount > 0:
                        #save ORIGINAL
                        cv2.imwrite(save_path_original, image_original)
                        #save ALL BOXES
                        cv2.imwrite(save_path_all_boxes, image_np)
                        detectedImagesCount += 1
                        file_sql_query = file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('" + save_path_original + "','" + file_date.sqlFormat(
                        ) + "'," + str(shop_id) + ");"
                        file_sql_query = file_sql_query + "INSERT INTO files (path,date,shop_id) VALUES ('" + save_path_all_boxes + "','" + file_date.sqlFormat(
                        ) + "'," + str(shop_id) + ");"

                    #drop source file
                    #os.remove(file_full_path) # <============== move
                    file_full_path_new = processed_path + shop_names[
                        shop_id] + "_" + files[file_id]
                    os.rename(file_full_path, file_full_path_new)

                    # == Save filename to sql
                    move_sql_query = "INSERT INTO files (path,date,shop_id) VALUES ('" + file_full_path_new + "','" + file_date.sqlFormat(
                    ) + "'," + str(shop_id) + ");"
                    cursor.execute(move_sql_query)
                    conn.commit()

                bar.finish()
                send_to_telegram(
                    chat,
                    str(detectedImagesCount) + " detected in " +
                    shop_names[shop_id])

            print("job complete. normal exit")
            send_to_telegram(chat, "job complete. normal exit")
import shutil

chat = "-1001448066127"

if host_check("scriptlab.net"):
    print("scriptlab.net - Ok")
else:
    print("scriptlab.net - Unavailable. Exit")
    exit()

if host_check("10.2.4.85"):
    print("10.2.4.85 (source images) - ok")
else:
    print("10.2.4.85 (source images) - Unavailable. Exit")
    send_to_telegram(
        chat,
        "10.2.4.85 (source images) - Unavailable. Unable to terminate records. Exit"
    )
    exit()

if host_check("10.2.5.191"):
    print("10.2.5.191 (destination path) - ok")
else:
    print("10.2.5.191 (destination path) - Unavailable. Exit")
    send_to_telegram(
        chat,
        "10.2.5.191 (destination path) - Unavailable. Unable to terminate records. Exit"
    )
    exit()


def load_image_into_numpy_array(image):
Пример #8
0
                    '======================================== SAMPLE 0 ========================================',
                    '')
                lex_result = lex_result.replace(
                    '================================================================================',
                    '')
                lex_result = lex_result.replace(lex_source, '')
                lex_result = lex_result.replace('@gpt2robot', '')
                lex_result = '.'.join(
                    lex_result.split('.')[:3]) + '.'  #crop left 4 sentences
                #translate
                if source_language != 'en':
                    lex_result = translate_text(source_language, lex_result)
                #send to telegram
                #with open("chat.txt",'r') as lex_file:
                #    chat=lex_file.read()
                send_to_telegram(chat, lex_result, message_id)
                print('waiting..')
            #else:
            #    lex_file.close()
        #lex--
        #logger.info("Generating predictions...")
        #predict_fn(network, text, params)
        sys.exit()

    # Train eval loop
    while True:
        start = time.time()

        network.train(input_fn=partial(input_fn, eval=False),
                      steps=params["train_steps"])