コード例 #1
0
def run_file(q, file):
    cam = cv2.VideoCapture(file)
    frame_num = 1
    time.sleep(2)
    success = True
    while success == True:
        reading_start = time.time()
        success, image = cam.read()
        if success:
            # Our operations on the image come here

            # convert image to cv2 format to RGB format
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            image = Image.fromarray(image)
            image = image.resize((224, 224))
            image_array = np.asarray(image)

            # Add resulting frame to Queue for processing on controller
            q.put(image_array)

            image = image.resize((672, 672))
            image_array = np.asarray(image)
            # Display the resulting frame
            cv2.imshow('frame', image_array)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            time.sleep(0.045)

        frame_num += 1
        #print("VPP Iteration Duration -> %s seconds ---" % (time.time() - reading_start))

    # When everything done, release the capture
    cam.release()
    cv2.destroyAllWindows()
 def imgToBytes(image):
     assert isinstance(image, Image.Image) and 'give me correct PIL Image'
     # resize
     data = io.BytesIO()
     image.resize(size=(IMAGE_SIZE, IMAGE_SIZE),
                  resample=Image.LANCZOS).save(data, 'JPEG', quality=90)
     return data.getvalue()
コード例 #3
0
def load_images_test(name, size):
    x_images = []
    y_images = []
    for file in tqdm(os.listdir(name)):
        image = Image.open(name + file)
        if image.mode != "RGB":
            image.convert("RGB")
        x_image = image.resize(
            (size[0] // 2,
             size[1] // 2))  #change to 3, 4, or 8 for varying scale factors
        x_image = x_image.resize(size, Image.BICUBIC)
        x_image = np.array(x_image)
        y_image = image.resize(size)
        y_image = np.array(y_image)
        x_images.append(x_image)
        y_images.append(y_image)
    x_images = np.array(x_images)
    y_images = np.array(y_images)
    x_images = x_images / 255
    y_images = y_images / 255
    x_images = x_images.reshape(
        x_images.shape[0], size[0] // 1, size[1] // 1,
        3)  #for grayscale, change the number of channels to 1
    y_images = y_images.reshape(
        y_images.shape[0], size[0] // 1, size[1] // 1,
        3)  #for grayscale, change the number of channels to 1
    return x_images, y_images
コード例 #4
0
ファイル: training4.py プロジェクト: aunchable/cs148
def processImage(imagePath):
    image = Image.open(imagePath)
    (currw, currh) = image.size
    if currw >= currh:
        image = image.resize((width, int(float(currh) * float(width) / float(currw))))
    else:
        image = image.resize((int(float(currw) * float(height) / float(currh)), height))
    background = Image.new('RGB', (width, height), (0, 0, 0))
    background.paste(
        image, (int((width - image.size[0]) / 2), int((height - image.size[1]) / 2))
    )
    return background
コード例 #5
0
def predictionImage():
    # remove warnins for compile model after loading
    with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
        test_model = load_model('../model/best_model.h5')

    # open image on input path
    with open('binarizedImage.jpg', 'r') as f:
        with Image.open(f).convert('L') as image:

            # change size of binarized image to 28x28 pixels
            resized_image = image.resize((28, 28), Image.ANTIALIAS)
            plt.imshow(resized_image)
            plt.show()

            # convert image to array
            x = img_to_array(resized_image)

            # add one dimension on image, [28, 28] -> [1, 28, 28]
            x = np.expand_dims(x, axis=0)

            # get predictions for all outputs(numbers)
            predictions = test_model.predict_classes(x)
            probabilities = test_model.predict_proba(x)

            # write data on output
            print("Number is: " + str(predictions))

            # remove image from disc
            os.remove('binarizedImage.jpg')
コード例 #6
0
ファイル: app2.py プロジェクト: kainow/hanbetucloud7
def upload_file():
    if request.method == 'POST':
        if 'file' not in request.files:
            print('ファイルがアプロードされていません')
            return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            print('ファイルがアプロードされていません')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

            filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)

            model = load_model('models/cloudcnn.h5')
            model = model_from_json(open('models/cloudmodel.json').read())

            image = Image.open(filepath)
            image = image.convert('RGB')
            image = image.resize((image_size, image_size))
            data = np.asarray(image)
            X = []
            X.append(data)
            X = np.array(X)

            result = model.predict([X])[0]
            predicted = result.argmax()
            percentage = int(result[predicted] * 100)

            return flash("その雲はおそらく" + classes[predicted] + ",で、その確率は" +
                         str(percentage) + "%ぐらいです!")
def preprocess_img(image):
    img = image.resize((80, 80))
    img = np.array(img, dtype="float32")
    img /= 255
    img = img.reshape(1, 80, 80, 3)

    return img
コード例 #8
0
    def _extract_face(self, filename):
        """[summary]

        Args:
            filename ([type]): [description]

        Returns:
            [type]: [description]
        """
        if self._model_name == 'facenet_keras.h5':
            required_size = (160, 160)
        else:
            required_size = (200, 200)
        # load image from file
        image = Image.open(filename)
        # convert to RGB, if needed
        image = image.convert('RGB')
        # convert to array
        pixels = np.asarray(image)
        # create the detector, using default weights
        detector = MTCNN()
        # detect faces in the image
        results = detector.detect_faces(pixels)
        # extract the bounding box from the first face
        x1, y1, width, height = results[0]['box']
        # deal with negative pixel index
        x1, y1 = abs(x1), abs(y1)
        x2, y2 = x1 + width, y1 + height
        # extract the face
        face = pixels[y1:y2, x1:x2]
        # resize pixels to the model size
        image = Image.fromarray(face)
        image = image.resize(required_size)
        face_array = np.asarray(image)
        return face_array
コード例 #9
0
def predict():
    # initialize the data dictionary that will be returned from the view
    response = {"success": False}

    # ensure an image was properly uploaded to our endpoint
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            # read the image in PIL format
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))
            image = image.convert("RGB")
            image = image.resize((image_w, image_h))

            image = np.expand_dims(image, axis=0)
            image = preprocess_input(image)

            pred = model.predict(image)
            index = np.argmax(pred)
            pred_value = foods_sorted[index]

            response["predictions"] = pred_value
            response["success"] = True

    # return the data dictionary as a JSON response
    return flask.jsonify(response)
コード例 #10
0
 def generate_features(self,
                       input_feature: list = None,
                       model: str = None,
                       version: int = None,
                       type_input: str = "json"):
     """
     Feature generation
     :param input_feature: list with input features
     :param model: name model
     :param version: version model
     """
     self.logger.info("Generate features for model")
     if input_feature is None:
         input_feature = []
     if type_input == "json":
         # TODO: for specific task
         self.feature = {"inputs": input_feature}
     if type_input == "file":
         in_memory_file = BytesIO()
         input_feature.save(in_memory_file)
         image = Image.open(in_memory_file)
         image = image.resize(self.target_size, Image.ANTIALIAS)
         img_array = np.array(image)
         img_array = np.expand_dims(img_array, axis=0)
         img_array = preprocess_input(img_array)
         self.feature = {"inputs": {"images": img_array.tolist()}}
コード例 #11
0
ファイル: app.py プロジェクト: c-soft/camera-classifier
def cover_status():

    ret_val = 'unknown'

    rtsp_url = app.rtsp_url

    cap = cv2.VideoCapture(rtsp_url)

    if not cap.isOpened():
        app.logger.error("Failed to open stream!")
        return ret_val

    ret, frame = cap.read()
    if ret == False:
        app.logger.error("Failed to read stream!")
        return ret_val

    img_cropped = frame[400:1080, 100:1300]

    image = Image.fromarray(cv2.cvtColor(img_cropped, cv2.COLOR_BGR2RGB))
    image = image.resize((320, 170))
    input_arr = keras.preprocessing.image.img_to_array(image)
    input_arr /= 255.0
    input_arr = np.array([input_arr])  # Convert single image to a batch.

    classes = (app.model.predict(input_arr) > 0.5).astype("int32")

    ret_val = "on" if classes[0][0] == 1 else "off"

    return ret_val
コード例 #12
0
def extract_face(filename, required_size=(224, 224)):
    # load image from file
    pixels = face_recognition.load_image_file(filename)
    # extract the bounding box from the first face
    face_bounding_boxes = face_recognition.face_locations(pixels)

    # 可能返回 >0, 多个人脸
    if len(face_bounding_boxes) == 0:
        return [], []

    face_list = []
    for face_box in face_bounding_boxes:
        top, right, bottom, left = face_box
        x1, y1, width, height = left, top, right - left, bottom - top
        x2, y2 = x1 + width, y1 + height
        # extract the face
        face = pixels[y1:y2, x1:x2]
        # resize pixels to the model size
        image = Image.fromarray(face)
        image = image.resize(required_size)
        face_array = np.asarray(image, 'float32')
        face_list.append(face_array)

        # show face
        #from PIL import ImageDraw
        #draw = ImageDraw.Draw(image)
        #del draw
        #image.show()

    return face_list, face_bounding_boxes
コード例 #13
0
def LoadImageFromBytes(bytes):
    image = Image.open(io.BytesIO(bytes)).convert('L')
    image = image.resize((28, 28))
    image = numpy.asarray(image, dtype='float32')
    image = image.reshape((1, image.shape[0], image.shape[1], 1))
    image /= 255
    return image
コード例 #14
0
def load_data():
  test_path = PATH+'/data/valid/'
  train_path = PATH+'/data/train/'
  x_train = []
  x_test = []
  classes = []

  cnt = 0
  for subdir, dir, files in os.walk(train_path):
    classes.append(dir)
    for file in files:
      img_path = os.path.join(subdir, file)
      image = Image.open(img_path)
      resize = image.resize((150,150), Image.NEAREST)
      resize.load()
      data = np.asarray( resize, dtype="uint8" )
      x_train.append(data)

  # for subdir, dir, files in os.walk(test_path):
  #   for file in files:
  #     img_path = os.path.join(subdir, file)
  #     x = image.load_img(img_path)
  #     x_test.append(x)

  x_train = np.array(x_train)
  x_test = np.array(x_test)
  return x_train, x_test
コード例 #15
0
def preprocess_image(image,target_size):
    if image.mode !="RGB":
        image=image.convert("RGB")
    image = image.resize(target_size)
    image = img_to_array(image)
    image = np.expand_dims(image,axis=0)
    return image
コード例 #16
0
def prepare_image(image, target):

    image = image.resize(target)
    image = img_to_array(image)
    image = image / 255
    image = np.expand_dims(image, axis=0)

    return image
コード例 #17
0
def preprocess_image(image, target_size):
    if image.mode != 'RGB':
        image = image.convert("RGB")
    image = image.resize(target_size)
    image = img_to_array(image)
    image = image.reshape(1, target_size[0], target_size[1], 3)
    print(image)
    return image
コード例 #18
0
def preprocess_image(image, target_size):
  if image.mode != "RGB":
      image = image.convert("RGB")
  image = image.resize(target_size)
  image = img_to_array(image)   # to numpy array
  image= np.expand_dims(image, axis=0)  # expands the dimensions of img

  return image    # preprocessed image returned to be passed to keras model
コード例 #19
0
ファイル: app.py プロジェクト: TushaarBedi/Learning
def prepare_image(image):
    if image.mode != "RGB":
        image = image.convert("RGB")
    image_size = (28, 28)
    image = image.resize(image_size)
    image = img_to_array(image)[:, :, 0]
    image /= 255
    image = 1 - image
    return image.reshape((1, 28, 28, 1))
コード例 #20
0
def prepare_image(image, target):
    # resize the input image and preprocess it
    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = imagenet_utils.preprocess_input(image)

    # return the processed image
    return image
コード例 #21
0
def preprocess_image(image,target_size=(26,26)):
    if image.mode != "L":
        image = image.convert("L") #Convert to grayscale
    image = PIL.ImageOps.invert(image) # Flip Black/white pixels
    image = image.resize(target_size) # Size appropriately
    arr = img_to_array(image)
    arr = np.expand_dims(arr, axis=0)
    arr /= 255.
    return arr
コード例 #22
0
    def adequacao_imagem(self, image, target_size):

        if image.mode != "RGB":
            image = image.convert("RGB")
        image = image.resize(target_size)
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)

        return image
コード例 #23
0
def load_images_train(name, size):
    x_images = []
    y_images = []
    for file in tqdm(os.listdir(name)):
        image = Image.open(name + file)
        if image.mode != "RGB":  # comment these lines if using grasycale images
            image.convert(
                "RGB")  # comment these lines if using grasycale images
        #train for multiple scale factors
        x_image1 = image.resize((size[0] // 2, size[1] // 2))
        x_image1 = x_image1.resize(size, Image.BICUBIC)
        x_image2 = image.resize((size[0] // 3, size[1] // 3))
        x_image2 = x_image2.resize(size, Image.BICUBIC)
        x_image3 = image.resize((size[0] // 4, size[1] // 4))
        x_image3 = x_image3.resize(size, Image.BICUBIC)
        x_image4 = image.resize((size[0] // 8, size[1] // 8))
        x_image4 = x_image4.resize(size, Image.BICUBIC)

        x_image1 = np.array(x_image1)
        y_image1 = image.resize(size)
        y_image1 = np.array(y_image1)
        x_images.append(x_image1)
        y_images.append(y_image1)

        x_image2 = np.array(x_image2)
        y_image2 = image.resize(size)
        y_image2 = np.array(y_image2)
        x_images.append(x_image2)
        y_images.append(y_image2)

        x_image3 = np.array(x_image3)
        y_image3 = image.resize(size)
        y_image3 = np.array(y_image3)
        x_images.append(x_image3)
        y_images.append(y_image3)

        x_image4 = np.array(x_image4)
        y_image4 = image.resize(size)
        y_image4 = np.array(y_image4)
        x_images.append(x_image4)
        y_images.append(y_image4)

    x_images = np.array(x_images)
    y_images = np.array(y_images)
    x_images = x_images / 255
    y_images = y_images / 255
    x_images = x_images.reshape(
        x_images.shape[0], size[0] // 1, size[1] // 1,
        3)  #for grayscale, change the number of channels to 1
    y_images = y_images.reshape(
        y_images.shape[0], size[0] // 1, size[1] // 1,
        3)  #for grayscale, change the number of channels to 1
    return x_images, y_images
コード例 #24
0
def prepare_image(image):
    if image.mode != "RGB":
        image = image.convert("RGB")
    image_size = (299, 299)
    image = image.resize(image_size)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = preprocess_input(image)
    # return the processed image
    return image
コード例 #25
0
def load_images_test(folder):
  for i in range(18540,20549):
      if 'DS_Store' not in "hello":
        image = Image.open(os.path.join(folder,str(i)+".jpg"))
        if image is not None:
          h = image.size[0]
          w = image.size[1]
          image = np.array(image.resize((150,150)))
          image= image / 255.
          images_test.append(image)
コード例 #26
0
ファイル: app.py プロジェクト: TushaarBedi/Learning
def prepare_image(image):
    if image.mode != "RGB":
        image = image.convert("RGB")
    image_size = (28, 28)
    image = image.resize(image_size)
    # image.save("fromform.png")
    image = img_to_array(image)[:, :, 0]
    image /= 255
    image = 1 - image
    return image.flatten().reshape(-1, 28 * 28)
コード例 #27
0
def prepare_image(image, target):
    if image.mode != 'RGB':
        image = image.convert('RGB')

    resized_img = image.resize(target)
    img_as_array = image.img_to_array(resized_img)
    expanded_img = np.expand_dims(img_as_array, axis=0)
    processed_img = preprocess_input(expanded_img)

    return processed_img
コード例 #28
0
ファイル: image_processor.py プロジェクト: perchrib/Face
def prepare_image(image, size):
    old_size = image.size
    ratio = float(size) / max(old_size)

    new_size = tuple([int(x * ratio) for x in old_size])
    old_im = image.resize(new_size, Image.ANTIALIAS)
    new_im = Image.new("RGB", (size, size))
    new_im.paste(old_im,
                 ((size - new_size[0]) // 2, (size - new_size[1]) // 2))

    return new_im
コード例 #29
0
def preprocess_image(image, target):
    if image.mode != 'RGB':
        image = image.convert("RGB")
    # resize image input and preprocess it
    image = image.resize(target)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = preprocess_input(image)

    # return the processed image
    return image
コード例 #30
0
ファイル: main.py プロジェクト: Sho-cmyk/shiny-winner
def upload_file():

    global graph
    with graph.as_default():
        if request.method == 'POST':

            myfile = request.form['snapShot'].split(',')
            imgdata = base64.b64decode(myfile[1])
            image = Image.open(io.BytesIO(imgdata))

            # 保存
            basename = datetime.now().strftime("%Y%m%d-%H%M%S")
            image.save(os.path.join(UPLOAD_FOLDER, basename + ".png"))
            filepath = os.path.join(UPLOAD_FOLDER, basename + ".png")

            image = image.convert('RGB')
            image = image.resize((image_size, image_size))
            data = np.asarray(image)
            X = []
            X.append(data)
            X = np.array(X).astype('float32')
            X /= 256

            result = model.predict([X])[0]
            result_proba = model.predict_proba(X, verbose=1)[0]

            percentage = (result_proba * 100).astype(float)
            array_sort = sorted(list(zip(percentage, classes, classes_img)),
                                reverse=True)
            percentage, array_class, array_img = zip(*array_sort)

            pred_answer1 = "ラベル:" + \
                str(array_class[0]) + ",確率:"+str(percentage[0])+"%"
            pred_answer2 = "ラベル:" + \
                str(array_class[1]) + ",確率:"+str(percentage[1])+"%"
            pred_answer3 = "ラベル:" + \
                str(array_class[2]) + ",確率:"+str(percentage[2])+"%"

            img_src1 = array_img[0]
            img_src2 = array_img[1]
            img_src3 = array_img[2]

            basename = datetime.now().strftime("%Y%m%d-%H%M%S")
            filepath3 = UPLOAD_FOLDER + basename + ".png"

            return render_template("index.html",
                                   answer1=pred_answer1,
                                   img_data1=img_src1,
                                   answer2=pred_answer2,
                                   img_data2=img_src2,
                                   answer3=pred_answer3,
                                   img_data3=img_src3)

        return render_template("index.html", answer="")