Пример #1
0
def main():
    if not os.path.isdir(flag.output_dir):
        os.mkdir(flag.output_dir)
    if flag.mode == 'train':
        train_op = train.TrainModel(flag)
        train_op.train()
    elif flag.mode == 'predict_img':
        predict_image(flag)
    elif flag.mode == 'predict_imgDir':
        print ('not supported')
    else:
        print ('not supported')
Пример #2
0
def predict_image_handler(project=None, publishedName=None):
    try:
        """
        imageData = None
        if ('imageData' in request.files):
            imageData = request.files['imageData']
        elif ('imageData' in request.form):
            imageData = request.form['imageData']
        else:
            imageData = io.BytesIO(request.get_data())
        """
        
        imageStr= request.get_data(as_text=True)
        if imageStr.startswith('b\''):
            encoded_image = encoded_image[2:-1]
        else:
            encoded_image = imageStr

        decoded_img = base64.b64decode(encoded_image.encode('utf-8'))
        img_buffer  = io.BytesIO(decoded_img)

        img = Image.open(io.BytesIO(decoded_img))
        results = predict_image(img)
        return jsonify(results['predictions'])
    
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #3
0
def main():
    current_dir = Path(__file__).resolve().parent
    cascade_path = Path(current_dir).joinpath(
        'cascade', 'haarcascade_frontalface_default.xml')

    # 画像データがbase64形式で標準出力に来る
    upload_data = sys.stdin.readline()
    img = base64_to_ndarray(upload_data)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    face_cascade = cv2.CascadeClassifier(str(cascade_path))
    faces = face_cascade.detectMultiScale(gray, 1.3, 3)

    images_dict = {}
    for i, (x, y, w, h) in enumerate(faces):
        images_dict[i] = {}
        face_ndarray = img[y:y + h, x:x + w]

        # リサイズしてbase64に変換
        face_ndarray = cv2.resize(face_ndarray, (128, 128))
        face_base64 = ndarray_to_base64(face_ndarray)

        images_dict[i]['data'] = face_base64
        images_dict[i]['score'] = predict.predict_image(face_ndarray)
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (147, 112, 216), 3)

    images_dict['original'] = ndarray_to_base64(img)
    print(json.dumps(images_dict))
Пример #4
0
def predict_image_handler():
    try:
        imageData = None
        if ('imageData' in request.files):
            imageData = request.files['imageData']
        else:
            imageData = io.BytesIO(request.get_data())

        img = Image.open(imageData)
        results = predict_image(img)
        prediction = MESSAGE_PARSER.highestProbabilityTagMeetingThreshold(results, THRESHOLD)
        file.write('\n------------\n')
        file.write('Got ' + prediction + '\n')
        file.write('------------\n')
        try:
            predictJson = json.loads(json.dumps(results))
            for item in predictJson:
                print(predictJson)
                file.write(item['Tag'] + ': ' + str(item['Probability']) + '\n')
        except Exception as e:
            print('!!! EXCEPTION:', str(e))
        file.flush()
        return json.dumps(results)
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #5
0
def predict_single_image(img_number):

    #dataset_path = '../data/lausanne'
    #dataset_path = '/scratch1/xkv467/lausanne'
    dataset_path = '/scratch1/xkv467/lausanne_unregistered'
    #dataset_path = '../data/test_dataset'
    results_path = '../results'
    batch_size = 128
    #img_class_map = [[0, 3, 4, 5, 6, 7, 8], [1,2]]
    img_class_map = [[0, 7], [1], [2], [3, 4, 8], [6], [5]]
    #img_class_map = [[0, 1, 2, 5, 6, 7], [3, 4, 8]] # other, vesicles
    #img_class_map = [[0, 7], [1, 2, 3, 4, 8, 6, 5]] # cytosol, rest


    output_size = len(img_class_map)

    norm_params = (126.04022903600975, 29.063149797089494)

    model_manager = ModelsManager(results_path)

    saved_model = model_manager.get_model('conv_2_layer_07_1_2_348_6_5_more_reg')

    model = saved_model.load_model('best')

    epoch, train, test = saved_model.session_stats()

    input_shape = saved_model.input_shape

    dataset = PatchDataset(dataset_path,
                           batch_size,
                           input_shape,
                           img_class_map,
                           predict_only=True
                           )

    # consistency test:
    # Does model produce the same test acc as expected two times in a row?

    #test_acc = test_model(dataset, model)
    #print('test_acc:', test_acc)

    image = get_image_stack(img_number, input_shape[0])

    output = predict_image(dataset, model, image)
    #print(output.shape)

    #plt.imshow(image[image.shape[0]//2,:,:], cmap='gray')

    #segments = np.argmax(output, axis=2)

    #colors = ['red', 'blue', 'g', 'purple', 'cyan', 'gold']

    #for class_idx in range(6):
    #    if class_idx == 1:
    #        pass
            #plt.contour((segments==class_idx).astype(int), levels=[0.5], colors = [colors[class_idx]], linewidths=1)

    #plt.show()

    return output
Пример #6
0
def predict_image_base64(encoded_image):
    if encoded_image.startswith('b\''):
        encoded_image = encoded_image[2:-1]

    decoded_img = base64.b64decode(encoded_image.encode('utf-8'))
    img_buffer = io.BytesIO(decoded_img)

    image = Image.open(io.BytesIO(decoded_img))
    return predict_image(image)
Пример #7
0
def predict_image_base64(encoded_image):
    if encoded_image.startswith('b\''):
        encoded_image = encoded_image[2:-1]

    decoded_img = base64.b64decode(encoded_image.encode('utf-8'))
    img_buffer  = io.BytesIO(decoded_img)

    image = Image.open(io.BytesIO(decoded_img))
    return predict_image(image)
Пример #8
0
def predict_image_handler(role):
    try:
        imageData = io.BytesIO(request.get_data())
        result = predict_image(imageData, role)
    
        return json.dumps(result)

    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #9
0
def home():
	if request.method == 'POST':
		image=request.files['image']
		image_string = base64.b64encode(image.read());print(image_string);
		binary = base64.b64decode(image_string)
		img = np.asarray(bytearray(binary), dtype="uint8")
		img = cv2.imdecode(img, 1)
		img = cv2.resize( img, (32,32), interpolation=cv2.INTER_AREA );pred = predict_image(img.reshape(1,32,32,3));
		return render_template('index.html', data={ 'status':True, 'img': str(image_string)[2:-1], 'pred': pred, 'accuracy':accuracy()})
	else:
		return render_template('index.html', data={ 'status':False, 'accuracy':accuracy() })
Пример #10
0
def main():

    if "train" not in os.listdir():
        os.mkdir("./train")

    files = glob.glob('./train/*.jpg')
    numOfFiles = len(files)
    rd = random.randint(0, numOfFiles)
    tf.app.flags.DEFINE_string('output_path',
                               './output/images/img_{}.jpg'.format(str(rd)),
                               'path to save image')
    tf.app.flags.DEFINE_string('input_path', files[rd],
                               'image to be predicted')

    if "output/images" not in os.listdir():
        os.mkdir("./output/images")

    # run prediction on random image in the images folder if no image path is specified
    if FLAGS.input_path == 'random':
        FLAGS.input_path = files[rd]
    img = cv2.imread(FLAGS.input_path)
    img = cv2.resize(img, (512, 512))
    results = predict_image(img)

    for i in range(len(results['predictions'])):
        name = results['predictions'][i]['tagName']
        prob = results['predictions'][i]['probability']
        x = np.int(results['predictions'][i]['boundingBox']['left'] *
                   img.shape[0])
        y = np.int(results['predictions'][i]['boundingBox']['top'] *
                   img.shape[1])
        w = x + np.int(
            results['predictions'][i]['boundingBox']['width'] * img.shape[0])
        h = y + np.int(
            results['predictions'][i]['boundingBox']['height'] * img.shape[1])
        img = cv2.rectangle(img, (x, y), (w, h),
                            color=(0, 255, 0),
                            thickness=2)
        img = cv2.putText(img,
                          name, (x, y - 15),
                          fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                          fontScale=0.5,
                          color=(0, 0, 255),
                          thickness=2)
        img = cv2.putText(img,
                          str(prob), (x, y - 35),
                          fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                          fontScale=0.5,
                          color=(0, 0, 255),
                          thickness=2)
    cv2.imshow('sh', img)
    cv2.imwrite(FLAGS.output_path, img)
    cv2.waitKey(0)
def upload_image():
    if request.method == "POST":
        image_file = request.files['image']
        if image_file:
            image_location = os.path.join(config.UPLOAD_FOLDER,
                                          image_file.filename)
            image_file.save(image_location)
            pred = predict_image(image_path=image_location)
            print(pred)
            return render_template("index.html",
                                   prediction=pred,
                                   image_loc=image_file.filename)
    return render_template("index.html", prediction=0, image_loc=None)
Пример #12
0
def predict_image_handler():
    try:
        imageData = None
        if ('imageData' in request.files):
            imageData = request.files['imageData']
        else:
            imageData = io.BytesIO(request.get_data())

        #img = scipy.misc.imread(imageData)
        img = Image.open(imageData)
        results = predict_image(img)
        return json.dumps(results)
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #13
0
def upload():
    if request.method == 'POST' and 'photo' in request.files:
        # filename = photos.save(request.files['photo'])
        img = Image.open(request.files['photo'].stream)
        # os.rename('./'+filename,'./'+'output.png')

    print("debug")
    #read the image into memory
    # img = Image.open('./output.png')
    results = predict_image(img)

    print(results)

    # return render_template("index2.html",s1 = s1, s2 = s2, s3 = s3,s4 = s4,s5 = s5,s6 = s6)
    return render_template("index2.html", results=results)
Пример #14
0
def predict_image_handler(project=None):
    try:
        imageData = None
        if ('imageData' in request.files):
            imageData = request.files['imageData']
        elif ('imageData' in request.form):
            imageData = request.form['imageData']
        else:
            imageData = io.BytesIO(request.get_data())

        img = Image.open(imageData)
        results = predict_image(img)
        return jsonify(results)
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #15
0
def predict_image_handler(project=None, publishedName=None):
    try:
        imageData = None
        if ('imageData' in request.files):
            imageData = request.files['imageData']
        elif ('imageData' in request.form):
            imageData = request.form['imageData']
        else:
            imageData = io.BytesIO(request.get_data())

        img = Image.open(imageData)
        results = predict_image(img)
        return jsonify(results)
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #16
0
def process_single_file(filename, blob, dirname=""):

    print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    with open(
            '{dirname}/{filename}'.format(filename=filename, dirname=dirname),
            'rb') as image:
        # record = {"filename": filename,
        #      "blob": blob,
        #      "date": datetime.datetime.utcnow()}
        # records = mydb.records
        # record_id = records.insert_one(record).inserted_id

        r = predict_image(image)
        #RPC CALL PREDICTION USING DB URL
        tagName = r["predictions"][0]["tagName"]
        # tagName = "cow"
        if tagName == "cow":
            print(f'{dirname}/{filename}')
            push_start = time.time()
            container_id = int(filename.split(".")[0]) % 5 + 1
            print(f'test{container_id}')
            block_blob_service.create_blob_from_path(
                f'test{container_id}',
                f'{filename}',
                f'{dirname}/{filename}',
                content_settings=ContentSettings(content_type='image/jpeg'))
            # block_blob_service.create_blob_from_bytes(f'test{container_id}',
            #                                         f'{filename}',
            #                                         blob,
            #                                         content_settings=ContentSettings(content_type='image/jpeg'))

            push_end = time.time()
            print("push blob time: {} sec".format(push_end - push_start))
            put_start = time.time()
            queue.put_message(
                "imagequeue",
                f"https://cowimagestorage.blob.core.windows.net/test{container_id}/"
                + filename)
            put_end = time.time()
            print("put message: {}".format(put_end - put_start))

        else:
            pass
        print(
            "------------------------------------------------------------------"
        )
Пример #17
0
def upload():
    pic = request.files['pic']
    if not pic:
        return 'No picture uploaded!', 400

    filename = secure_filename(pic.filename)

    if not filename:
        return 'Bad upload!', 400

    picture = pic.read()
    food = predict_image(picture)

    result = Storage(int(randint(0, 999999)), food)
    db.session.add(result)
    db.session.commit()

    return food
Пример #18
0
def upload_file():
    file = request.files['image']
    # Read image
    image = cv2.imdecode(np.fromstring(file.read(), np.uint8),
                         cv2.IMREAD_UNCHANGED)
    filename = 'dataset/predict/' + file.filename
    #image.save(filename)
    cv2.imwrite(filename, image)
    path = 'dataset/predict/' + file.filename
    image_result, name = predict.predict_image(path)
    image_content = cv2.imencode('.jpg', image_result)[1].tostring()
    encoded_image = base64.encodestring(image_content)
    to_send = 'data:image/jpg;base64, ' + str(encoded_image, 'utf-8')
    return render_template('index.html',
                           faceDetected=1,
                           num_faces=1,
                           image_to_show=to_send,
                           init=True,
                           name=name)
Пример #19
0
def predict_image_handler(project=None, publishedName=None):
    try:
        imageData = None
        if ('imageData' in request.files):
            imageData = request.files['imageData']
        elif ('imageData' in request.form):
            imageData = request.form['imageData']
        else:
            imageData = io.BytesIO(request.get_data())

        img = Image.open(imageData)
        results = predict_image(img)
        # get the most likely prediction
        highest_prediction = get_highest_prediction(results['predictions'])
        # replace list of all predictions with the highest one
        results['predictions'] = highest_prediction
        return jsonify(results)
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #20
0
def predict_image_handler(project=None):
    print("Image processing start")
    try:
        imageData = None
        if ('imageData' in request.files):
            print("ImageData as file found")
            imageData = request.files['imageData']
        elif ('imageData' in request.form):
            print("ImageData as form found")
            imageData = request.form['imageData']
        else:
            print("ImageData not present, continue with RAW data")
            imageData = io.BytesIO(request.get_data())

        img = Image.open(imageData)
        results = predict_image(img)
        return jsonify(results)
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
def predict_image_handler():
    global count
    try:
        print('data received')
        imageData = None
        if ('imageData' in request.files):
            imageData = request.files['imageData']
        else:
            imageData = io.BytesIO(request.get_data())

        #img = scipy.misc.imread(imageData)
        img = Image.open(imageData)
        count = count + 1

        # img.save('/images/image' + str(count) + '.jpg', "JPEG")

        results = predict_image(img)
        return json.dumps(results)
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #22
0
def classify(net, image, image_name):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # height, width, channels = image.shape
    file_root_test = 'VOCdevkit_2007/VOC2007test/JPEGImages/'
    result = predict_image(net, image, root_img_directory=file_root_test)
    _labels = []
    for left_up, right_bottom, class_name, _, prob in result:
        _labels.append(class_name)
        color = COLORS[VOC_CLASSES.index(class_name)]
        cv2.rectangle(image, left_up, right_bottom, color, 2)
        label = class_name + str(round(prob, 2))
        text_size, baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX,
                                              0.4, 1)
        p1 = (left_up[0], left_up[1] - text_size[1])
        cv2.rectangle(image, (p1[0] - 2 // 2, p1[1] - 2 - baseline),
                      (p1[0] + text_size[0], p1[1] + text_size[1]), color, -1)
        cv2.putText(image, label, (p1[0], p1[1] + baseline),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, 8)

    cv2.resize(image, (15, 15))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    cv2.imwrite(insert_dash(image_name), image)
    return _labels
Пример #23
0
    def predictButtonPressed(self):
        if not os.path.exists(self.imagePath.get()): return
        if not os.path.exists(self.modelPath.get()): return

        self.startLoading()

        # Load first image, preprocess second
        self.Image1Data = cv2.imread(self.imagePath.get())
        self.Image1Data = cv2.cvtColor(self.Image1Data, cv2.COLOR_BGR2RGB)
        self.showImage1()
        self.preprocessImage()
        self.showImage2()

        # Make Prediction (and Heatmaps if checked)
        if not hasattr(self, 'altMode') or not self.altMode:
            self.heatmaps = None
            buildHeatmaps = 'last' if self.buildHeatmapsChecked.get(
            ) == 1 else False
            self.predictions, self.prediction, _, self.heatmaps = predict.predict_image(
                self.modelPath.get(), 3, self.Image2Data, 250, buildHeatmaps)
            self.showPrediction()

        self.stopLoading()
Пример #24
0
def predict_image_handler():
    try:
        imageData = None
        if ('imageData' in request.files):
            imageData = request.files['imageData']
        else:
            imageData = io.BytesIO(request.get_data())

        #img = scipy.misc.imread(imageData)
        img = Image.open(imageData)
        results = predict_image(img)

        # local model
        highestProb = highestProbabilityTagMeetingThreshold(results, 0.3)

        # cloud model
        if highestProb < 0.6:
            results = analyze_image_external(img)
            print(results)

        return json.dumps(results)
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #25
0
def predict_image_handler(project=None, publishedName=None):
    global blob_service_client
    try:
        imageData = None
        if ('imageData' in request.files):
            imageData = request.files['imageData']
        elif ('imageData' in request.form):
            imageData = request.form['imageData']
        else:
            imageData = io.BytesIO(request.get_data())
        if blob_service_client == None:
            print("create blob_service_client")
            storage_name = request.args.get('storagename')
            storage_key = request.args.get('storagekey')
            url = "https://{}.blob.core.windows.net".format(storage_name)
            blob_service_client = BlobServiceClient(account_url=url,
                                                    credential=storage_key)

        img = Image.open(imageData)
        results = predict_image(img, blob_service_client)
        return jsonify(results)
    except Exception as e:
        print('EXCEPTION:', str(e))
        return 'Error processing image', 500
Пример #26
0
    async def recv(self):
        global d1
        global d2
        global d3
        global d4
        global d5
        global frameCount

        frame = await self.track.recv()

        if self.transform == "cartoon":
            img = frame.to_ndarray(format="bgr24")

            # prepare color
            img_color = cv2.pyrDown(cv2.pyrDown(img))
            for _ in range(6):
                img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
            img_color = cv2.pyrUp(cv2.pyrUp(img_color))

            # prepare edges
            img_edges = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
            img_edges = cv2.adaptiveThreshold(
                cv2.medianBlur(img_edges, 7),
                255,
                cv2.ADAPTIVE_THRESH_MEAN_C,
                cv2.THRESH_BINARY,
                9,
                2,
            )
            img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)

            # combine color and edges
            img = cv2.bitwise_and(img_color, img_edges)

            # rebuild a VideoFrame, preserving timing information
            new_frame = VideoFrame.from_ndarray(img, format="bgr24")
            new_frame.pts = frame.pts
            new_frame.time_base = frame.time_base
            return new_frame
        elif self.transform == "edges":
            # perform edge detection
            img = frame.to_ndarray(format="bgr24")
            img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)

            # rebuild a VideoFrame, preserving timing information
            new_frame = VideoFrame.from_ndarray(img, format="bgr24")
            new_frame.pts = frame.pts
            new_frame.time_base = frame.time_base
            return new_frame
        elif self.transform == "rotate":
            # rotate image
            img = frame.to_ndarray(format="bgr24")
            rows, cols, _ = img.shape
            M = cv2.getRotationMatrix2D((cols / 2, rows / 2), frame.time * 45,
                                        1)
            img = cv2.warpAffine(img, M, (cols, rows))

            # rebuild a VideoFrame, preserving timing information
            new_frame = VideoFrame.from_ndarray(img, format="bgr24")
            new_frame.pts = frame.pts
            new_frame.time_base = frame.time_base
            return new_frame
        else:
            t1 = time.time()

            img = frame.to_ndarray(format="bgr24")
            rows, cols, _ = img.shape
            t2 = time.time()

            dec = Image.fromarray(frame.to_ndarray(format="rgb24"), mode="RGB")
            t3 = time.time()

            result = predict_image(dec)
            t4 = time.time()

            # for prediction in result['predictions']:
            if len(result['predictions']) > 0:
                prediction = sorted(result['predictions'],
                                    key=lambda i: i['probability'])[0]

                x = int(cols * prediction['boundingBox']['left'])
                y = int(rows * prediction['boundingBox']['top'])
                w = int(cols * prediction['boundingBox']['width'])
                h = int(rows * prediction['boundingBox']['height'])

                cv2.rectangle(img, (x, y), (x + w, y + h), (36, 255, 12), 1)

                cv2.putText(
                    img, "{} ({}%)".format(prediction['tagName'],
                                           prediction['probability'] * 100),
                    (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12),
                    2)

            t5 = time.time()

            new_frame = VideoFrame.from_ndarray(img, format="bgr24")
            t6 = time.time()
            new_frame.pts = frame.pts
            new_frame.time_base = frame.time_base

            d1 = d1 + (t2 - t1)
            d2 = d2 + (t3 - t2)
            d3 = d3 + (t4 - t3)
            d4 = d4 + (t5 - t4)
            d5 = d5 + (t6 - t5)
            frameCount = frameCount + 1
            if frameCount > 9:
                print("*****")
                print("*****")
                print("***** Average decode1 {}ms".format(d1 * 100))
                print("***** Average decode2 {}ms".format(d1 * 100))
                print("***** Average predict {}ms".format(d3 * 100))
                print("***** Average draw {}ms".format(d4 * 100))
                print("***** Average encode {}ms".format(d5 * 100))
                print("*****")
                print("*****")
                d1 = 0
                d2 = 0
                d3 = 0
                d4 = 0
                d5 = 0
                frameCount = 0

            return new_frame
        torch.save(net.state_dict(), 'best_detector.pth')

    torch.save(net.state_dict(), 'detector.pth')

#View an example output
net.eval()
net.load_state_dict(torch.load('best_detector.pth'))
# select random image from train set
image_name = random.choice(train_dataset.fnames)
image = cv2.imread(os.path.join(file_root_train, image_name))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
threshold = 0.1
print('predicting...')
print(image.shape)
result = predict_image(net,
                       image_name,
                       root_img_directory=file_root_train,
                       threshold=threshold)
for left_up, right_bottom, class_name, _, prob in result:
    color = COLORS[VOC_CLASSES.index(class_name)]
    cv2.rectangle(image, left_up, right_bottom, color, 2)
    label = class_name + str(round(prob, 2))
    text_size, baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                          1)
    p1 = (left_up[0], left_up[1] - text_size[1])
    cv2.rectangle(image, (p1[0] - 2 // 2, p1[1] - 2 - baseline),
                  (p1[0] + text_size[0], p1[1] + text_size[1]), color, -1)
    cv2.putText(image, label, (p1[0], p1[1] + baseline),
                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, 8)

plt.figure(figsize=(15, 15))
plt.imshow(image)
Пример #28
0
            if n == "q":
                break

            start = time.time()
            camera.start_preview(alpha=200)
            camera.resolution = (1980, 1080)
            camera.framerate = 15
            #sleep(5)
            #sleep(1)
            camera.capture('image.jpg')

            camera.stop_preview()

            image = Image.open("image.jpg")

            result = predict_image(image)

            #print("Results from Custom Vision Model: ")
            #print(result)

            #print("Results from Custom Vision Model: Predictions ")
            #print(result['predictions'])
            print()

            i = 0
            for tag in result['predictions']:
                print("ID: " + str(tag['tagId']) + " Object detected : " +
                      tag['tagName'] + " Confidence: " +
                      '{0:f}'.format(tag['probability']))
                print()
Пример #29
0
import predict
import getopt
import os
import sys
from pathlib import Path

if __name__ == '__main__':
    options, args = getopt.getopt(sys.argv[1:], 'i:', ["lang="])
    for o, a in options:
        if o == '-i':
            dir = a
        elif o == "--lang":
            if a == 'en':
                source = 'en'
                target = 'ko'
            elif a == 'ko':
                source = 'ko'
                target = 'en'
            else:
                exit(-1)

    if dir == None: exit(-1)
    predict.predict_image(dir)
    new_dir = Path(dir).parent
    os.system(
        f"python ocr_refactored.py -i {new_dir}/tmp_text.png -m {new_dir}/tmp_masked.png -s {source} -t {target}"
    )
    os.system(f"rm {new_dir}/tmp*")
Пример #30
0
import predict

predict.predict_image('slider.png')
Пример #31
0
    if best_test_loss > test_loss:
        best_test_loss = test_loss
        print('Updating best test loss: %.5f' % best_test_loss)
        torch.save(net.state_dict(), '/results/best_detector.pth')

    torch.save(net.state_dict(), '/results/detector.pth')

net.eval()

# select random image from test set
image_name = random.choice(test_dataset.fnames)
image = cv2.imread(os.path.join(file_root_test, image_name))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

print('predicting...')
result = predict_image(net, image_name, root_img_directory=file_root_test)
for left_up, right_bottom, class_name, _, prob in result:
    color = COLORS[VOC_CLASSES.index(class_name)]
    cv2.rectangle(image, left_up, right_bottom, color, 2)
    label = class_name + str(round(prob, 2))
    text_size, baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                          1)
    p1 = (left_up[0], left_up[1] - text_size[1])
    cv2.rectangle(image, (p1[0] - 2 // 2, p1[1] - 2 - baseline),
                  (p1[0] + text_size[0], p1[1] + text_size[1]), color, -1)
    cv2.putText(image, label, (p1[0], p1[1] + baseline),
                cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, 8)

plt.figure(figsize=(15, 15))
plt.imshow(image)
def save_class_activation_map_evolutions(model_path,
                                         model_type,
                                         num_classes,
                                         image_path,
                                         image,
                                         image_size=250,
                                         output_dir="./results/cam/"):
    # Generate Prediction & CAM-Evolutions
    # NOTE: Ironically I use the function of another module which then calls `class_activation_map_evolutions`.
    #       But that's how it is... :D
    predictions, prediction, predicted_class, all_map_evolutions = predict.predict_image(
        model_path, model_type, image, image_size, 'all')

    # Superimpose weighted & labeled heatmaps on image
    all_map_evolutions_superimposed = []
    for num_class in range(0, num_classes):
        class_prediction = predictions[num_class]

        map_evolutions_superimposed = [label_image(image, "0 - Input")]
        weighted_map_evolutions_superimposed = [
            label_image(image, "0 - Input")
        ]

        for idx, heatmap in enumerate(all_map_evolutions[num_class]):
            superimposed_heatmap = superimpose_heatmap_on_image(image, heatmap)
            superimposed_heatmap = label_image(superimposed_heatmap,
                                               f"{idx+1} - Conv2D")
            map_evolutions_superimposed.append(superimposed_heatmap)

            superimposed_weighted_heatmap = superimpose_heatmap_on_image(
                image, heatmap * class_prediction)
            superimposed_weighted_heatmap = label_image(
                superimposed_weighted_heatmap, f"{idx+1} - Conv2D (Weighted)")
            weighted_map_evolutions_superimposed.append(
                superimposed_weighted_heatmap)

        all_map_evolutions_superimposed.append([
            map_evolutions_superimposed, weighted_map_evolutions_superimposed
        ])

    # Create Unique Output Directory
    output_dir = io.next_path(output_dir, create_dirs=True)

    # Generate & Save Weighted Heatmap
    # last_heatmaps = [all_map_evolutions[0][-1], all_map_evolutions[1][-1]]
    # superimposed_weighted_heatmap = superimpose_weighted_heatmap_on_image(image, last_heatmaps, predictions)
    # output_path = os.path.join(output_dir, f'CAM_weighted.png')
    # cv2.imwrite(output_path, cv2.cvtColor(superimposed_weighted_heatmap, cv2.COLOR_RGB2BGR))

    print(f"Save Animated GIF & Seperate PNGs..")
    for num_class in range(0, num_classes):
        output_dir_frames = os.path.join(output_dir,
                                         f"CAM_class_{num_class}_frames/")
        if not os.path.exists(output_dir_frames):
            os.makedirs(output_dir_frames)

        heatmaps_seq = all_map_evolutions_superimposed[num_class][0]
        weighted_heatmaps_seq = all_map_evolutions_superimposed[num_class][1]

        # Seperate PNGs
        for idx in range(len(heatmaps_seq)):
            heatmap = heatmaps_seq[idx]
            output_path = os.path.join(
                output_dir_frames, f'CAM_class_{num_class}_frame_{idx}.png')
            # cv2.imwrite(output_path, heatmap)
            cv2.imwrite(output_path, cv2.cvtColor(heatmap, cv2.COLOR_RGB2BGR))

            weighted_heatmap = weighted_heatmaps_seq[idx]
            weighted_output_path = os.path.join(
                output_dir_frames,
                f'CAM_class_{num_class}_weighted_frame_{idx}.png')
            # cv2.imwrite(weighted_output_path, weighted_heatmap)
            cv2.imwrite(weighted_output_path,
                        cv2.cvtColor(weighted_heatmap, cv2.COLOR_RGB2BGR))

        # Animated GIF
        output_path = os.path.join(output_dir,
                                   f'CAM_class_{num_class}_animated.gif')
        imageio.mimsave(output_path, heatmaps_seq, duration=0.5)

        output_path = os.path.join(
            output_dir, f'CAM_class_{num_class}_weighted_animated.gif')
        imageio.mimsave(output_path, weighted_heatmaps_seq, duration=0.5)

    # Write Info-File
    info_path = os.path.join(output_dir, "_INFO.txt")
    predicted_class_name = "Benign" if predicted_class == 0 else "Malignant"
    with open(info_path, "w") as text_file:
        print(f"Image-Path: {image_path}", file=text_file)
        print(f"Model-Path: {model_path}", file=text_file)
        print(f"Model-Type: {model_type}", file=text_file)
        print(f"Predicted Class: {predicted_class} ({predicted_class_name})",
              file=text_file)
        print(f"Exact Predictions (Weight Factors): {predictions}",
              file=text_file)
        print(f"Single Value Prediction: {prediction}", file=text_file)
        print(f"\nCommand:", file=text_file)
        print(
            f'python cam.py --model_path "{model_path}" --model_type {model_type} --image_path "{image_path}"',
            file=text_file)

    return output_dir