Python cv2 模块,imdecode() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.imdecode()

项目:chainer-cyclegan    作者:Aixile    | 项目源码 | 文件源码
def get_example(self, i):
        id = self.all_keys[i]
        img = None
        val = self.db.get(id.encode())

        img = cv2.imdecode(np.fromstring(val, dtype=np.uint8), 1)
        img = self.do_augmentation(img)

        img_color = img
        img_color = self.preprocess_image(img_color)

        img_line = XDoG(img)
        img_line = cv2.cvtColor(img_line, cv2.COLOR_GRAY2RGB)
        #if img_line.ndim == 2:
        #    img_line = img_line[:, :, np.newaxis]
        img_line = self.preprocess_image(img_line)

        return img_line, img_color
项目:yolo_light    作者:chrisgundling    | 项目源码 | 文件源码
def updateImage(self, img):
        arr = self.bridge.imgmsg_to_cv2(img,"bgr8") 
        # Uncomment following two lines for CompressedImage topic
        #np_arr = np.fromstring(img.data, np.uint8)
        #arr = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
        if self.image_lock.acquire(True):
            self.img = arr
            if self.model is None:
                self.model = self.get_model()
            self.img_out, self.boxes = self.predict(self.model, self.img)
            self.img_out = np.asarray(self.img_out[0,:,:,:])
            for box in self.boxes:
                if 'traffic light' in box['label']:
                    cv2.rectangle(self.img_out,(box['topleft']['x'], 
                                                box['topleft']['y']), 
                                                (box['bottomright']['x'], 
                                                box['bottomright']['y']), 
                                                (255,0,0), 6)
                    cv2.putText(self.img_out, box['label'], 
                               (box['topleft']['x'], 
                               box['topleft']['y'] - 12), 0, 0.6, (255,0,0) ,6//3)

            print(self.img_out.shape)
            self.image_lock.release()
项目:pycolor_detection    作者:parth1993    | 项目源码 | 文件源码
def predict(self, input_file):

        # img = base64.b64decode(input_base64)
        # img_array = np.fromstring(img, np.uint8)
        # input_file = cv2.imdecode(img_array, 1)

        # ip_converted = preprocessing.resizing(input_base64)
        segmented_image = preprocessing.image_segmentation(
                preprocessing.resizing(input_file)
            )
        # processed_image = preprocessing.removebg(segmented_image)
        detect = pycolor.detect_color(
                segmented_image,
                self._mapping_file
            )
        return (detect)
项目:ecs-mxnet-example    作者:awslabs    | 项目源码 | 文件源码
def predict(url, mod, synsets):
     req = urllib2.urlopen(url)
     arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
     cv2_img = cv2.imdecode(arr, -1)
     img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB)
     if img is None:
         return None
     img = cv2.resize(img, (224, 224))
     img = np.swapaxes(img, 0, 2)
     img = np.swapaxes(img, 1, 2)
     img = img[np.newaxis, :]

     mod.forward(Batch([mx.nd.array(img)]))
     prob = mod.get_outputs()[0].asnumpy()
     prob = np.squeeze(prob)

     a = np.argsort(prob)[::-1]
     out = ''
     for i in a[0:5]:
         out += 'probability=%f, class=%s' %(prob[i], synsets[i])
     out += "\n"
     return out
项目:pycreate2    作者:MomsFriendlyRobotCompany    | 项目源码 | 文件源码
def read():
    db = shelve.open(filename)
    imgs = db['imgs']
    data = db['data']

    for i in range(len(imgs)):
        d = data[i]
        print(i, d)
        img = imgs[i]
        img = np.fromstring(img, np.uint8)
        frame = cv2.imdecode(img, 1)
        print('frame[{}] {}'.format(i, frame.shape))
        cv2.imshow('camera', frame)
        cv2.waitKey(300)

    print('bye ...')
    cv2.destroyAllWindows()
    db.close()
项目:tf-lcnn    作者:ildoonet    | 项目源码 | 文件源码
def get_data(self):
        idxs = np.arange(len(self.train_list))
        if self.shuffle:
            self.rng.shuffle(idxs)

        caches = {}
        for i, k in enumerate(idxs):
            path = self.train_list[k]
            label = self.lb_list[k]

            if i % self.preload == 0:
                try:
                    caches = ILSVRCTenth._read_tenth_batch(self.train_list[idxs[i:i+self.preload]])
                except Exception as e:
                    logging.warning('tenth local cache failed, err=%s' % str(e))

            content = caches.get(path, '')
            if not content:
                content = ILSVRCTenth._read_tenth(path)

            img = cv2.imdecode(np.fromstring(content, dtype=np.uint8), cv2.IMREAD_COLOR)
            yield [img, label]
项目:blcf    作者:willard-yuan    | 项目源码 | 文件源码
def upload():
    # Get the name of the uploaded file
    file = request.files['file']
    # Check if the file is one of the allowed types/extensions
    if file and allowed_file(file.filename):
        # Make the filename safe, remove unsupported chars
        filename = secure_filename(file.filename)
        # Move the file form the temporal folder to
        # the upload folder we setup
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # Redirect the user to the uploaded_file route, which
        # will basicaly show on the browser the uploaded file
        # CV2
        #img_np = cv2.imdecode(np.fromstring(file.read(), np.uint8), cv2.IMREAD_UNCHANGED) # cv2.IMREAD_COLOR in OpenCV 3.1
        img_np = cv2.imread(os.path.join(app.config['UPLOAD_FOLDER'], filename), -1)
        cv2.imshow("Image", img_np)
        return redirect(url_for('uploaded_file',
                                filename=filename))

# This route is expecting a parameter containing the name
# of a file. Then it will locate that file on the upload
# directory and show it on the browser, so if the user uploads
# an image, that image is going to be show after the upload
项目:visualize-tsne    作者:YontiLevin    | 项目源码 | 文件源码
def url_to_img_array(url):
    if not isinstance(url, basestring):
        logging.warning("input is neither an ndarray nor a string, so I don't know what to do")
        return None

    # replace_https_with_http:
    if 'http' in url and 'https' not in url:
        url = url.replace("https", "http")
    try:
        headers = {'User-Agent': USER_AGENT}
        response = requests.get(url, headers=headers)
        img_array = cv2.imdecode(np.asarray(bytearray(response.content)), 1)
    except requests.ConnectionError:
        logging.warning("connection error - check url or connection")
        return None
    except:
        logging.warning(" error other than connection error - check something other than connection")
        return None

    return img_array
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def detectFromBlob(self, blob):
        '''
        Detect markers from web blob
        blob is image blob, type is bytes

        For example:
        >>> modelviews = webApp.detectFromBlob(blob)
        '''
        array = np.frombuffer(blob, np.uint8)
        if array.shape[0] < 1024: return {}
        frame = cv2.imdecode(array, 0)
        if frame is None: return {}
        frame = cv2.resize(frame, (self.args['PLAYER_RECT'][2], self.args['PLAYER_RECT'][3]))
        markers, area = (self.markerDetector.detect( frame, enFilter=True, enArea=True) or
                         ([], None))
        modelview_dict = {}
        for marker in markers:
            modelview_dict[marker.marker_id] = WebAPP.cvt2TJModelView(marker)
        return {'modelview': modelview_dict, 'area': area}
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def detectFromBlob(self, blob):
        '''
        Detect markers from web blob
        blob is image blob, type is bytes

        For example:
        >>> modelviews = webApp.detectFromBlob(blob)
        '''
        array = np.frombuffer(blob, np.uint8)
        if array.shape[0] < 1024: return {}
        frame = cv2.imdecode(array, 0)
        if frame is None: return {}
        frame = cv2.resize(frame, (self.args['PLAYER_RECT'][2], self.args['PLAYER_RECT'][3]))
        markers, area = (self.markerDetector.detect( frame, enFilter=True, enArea=True) or
                         ([], None))
        modelview_dict = {}
        for marker in markers:
            modelview_dict[marker.marker_id] = WebAPP.cvt2TJModelView(marker)
        return {'modelview': modelview_dict, 'area': area}
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def detectFromBlob(self, blob):
        '''
        Detect markers from web blob
        blob is image blob, type is bytes

        For example:
        >>> modelviews = webApp.detectFromBlob(blob)
        '''
        array = np.frombuffer(blob, np.uint8)
        if array.shape[0] < 1024: return {}
        frame = cv2.imdecode(array, 0)
        if frame is None: return {}
        frame = cv2.resize(frame, (self.args['PLAYER_RECT'][2], self.args['PLAYER_RECT'][3]))
        markers, area = (self.markerDetector.detect( frame, enFilter=True, enArea=True) or
                         ([], None))
        modelview_dict = {}
        for marker in markers:
            modelview_dict[marker.marker_id] = WebAPP.cvt2TJModelView(marker)
        return {'modelview': modelview_dict, 'area': area}
项目:BAR4Py    作者:bxtkezhan    | 项目源码 | 文件源码
def detectFromBlob(self, blob):
        '''
        Detect markers from web blob
        blob is image blob, type is bytes

        For example:
        >>> modelviews = webApp.detectFromBlob(blob)
        '''
        array = np.frombuffer(blob, np.uint8)
        if array.shape[0] < 1024: return {}
        frame = cv2.imdecode(array, 0)
        if frame is None: return {}
        frame = cv2.resize(frame, (self.args['PLAYER_RECT'][2], self.args['PLAYER_RECT'][3]))
        markers, area = (self.markerDetector.detect( frame, enFilter=True, enArea=True) or
                         ([], None))
        modelview_dict = {}
        for marker in markers:
            modelview_dict[marker.marker_id] = WebAPP.cvt2TJModelView(marker)
        return {'modelview': modelview_dict, 'area': area}
项目:telegram_robot    作者:uts-magic-lab    | 项目源码 | 文件源码
def get_image_compressed(self):
        rospy.loginfo("Getting image...")
        image_msg = rospy.wait_for_message(
            "/wide_stereo/left/image_raw/compressed",
            CompressedImage)
        rospy.loginfo("Got image!")

        # Image to numpy array
        np_arr = np.fromstring(image_msg.data, np.uint8)
        # Decode to cv2 image and store
        cv2_img = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR)
        img_file_path = "/tmp/telegram_last_image.png"
        cv2.imwrite(img_file_path, cv2_img)
        rospy.loginfo("Saved to: " + img_file_path)
        return img_file_path

    # Define a few command handlers
项目:telegram_robot    作者:uts-magic-lab    | 项目源码 | 文件源码
def get_image_compressed(self):
        rospy.loginfo("Getting image...")
        image_msg = rospy.wait_for_message(
            "/wide_stereo/left/image_raw/compressed",
            CompressedImage)
        rospy.loginfo("Got image!")

        # Image to numpy array
        np_arr = np.fromstring(image_msg.data, np.uint8)
        # Decode to cv2 image and store
        cv2_img = cv2.imdecode(np_arr, cv2.CV_LOAD_IMAGE_COLOR)
        img_file_path = "/tmp/telegram_last_image.png"
        cv2.imwrite(img_file_path, cv2_img)
        rospy.loginfo("Saved to: " + img_file_path)
        return img_file_path

    # Define a few command handlers
项目:Deep_Learning_In_Action    作者:iFighting    | 项目源码 | 文件源码
def __iter__(self):
        for k in range(self.count / self.batch_size):
            data = []
            label = []
            for i in range(self.batch_size):
                num = gen_rand()
                img = self.captcha.generate(num)
                img = np.fromstring(img.getvalue(), dtype='uint8')
                img = cv2.imdecode(img, cv2.IMREAD_COLOR)
                img = cv2.resize(img, (self.width, self.height))
                cv2.imwrite("./tmp" + str(i % 10) + ".png", img)
                img = np.multiply(img, 1/255.0)
                img = img.transpose(2, 0, 1)
                data.append(img)
                label.append(get_label(num))

            data_all = [mx.nd.array(data)]
            label_all = [mx.nd.array(label)]
            data_names = ['data']
            label_names = ['softmax_label']

            data_batch = OCRBatch(data_names, data_all, label_names, label_all)
            yield data_batch
项目:histonets-cv    作者:sul-cidr    | 项目源码 | 文件源码
def __init__(self, content=None, image=None):
        self.image = None
        self.format = None
        if isinstance(image, Image):
            self.image = image.image
            self.format = image.format
        elif image is not None:
            self.image = image
        elif content:
            image_format = imghdr.what(file='', h=content)
            if image_format is not None:
                image_array = np.fromstring(content, np.uint8)
                self.image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
                self.format = image_format
        if self.image is None:
            raise click.BadParameter('Image format not supported')
项目:FastRcnnDetect    作者:karthkk    | 项目源码 | 文件源码
def post(self):
        global detector
        imstrjpg = self.get_argument('data', 'empty')
        if imstrjpg == 'emtpy':
            print 'EMPTY'
            return ""
        imstr = np.fromstring(imstrjpg, dtype=np.uint8)
        im = cv2.imdecode(imstr, cv2.CV_LOAD_IMAGE_UNCHANGED)
        scores, boxes = detector.detect(im)
        CONF_THRESH = 0.15
        NMS_THRESH = 0.08
        results = {}
        for cls_ind, cls in enumerate(CLASSES[1:]):
            cls_ind += 1 # because we skipped background
            cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
            cls_scores = scores[:, cls_ind]
            dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
            keep = nms(dets, NMS_THRESH)
            dets = dets[keep, :]
            results[cls] = dets

        self.write(cPickle.dumps(results))
        self.finish()
项目:FastRcnnDetect    作者:karthkk    | 项目源码 | 文件源码
def post(self):
        global detector
        imstrjpg = self.get_argument('data', 'empty')
        if imstrjpg == 'emtpy':
            print 'EMPTY'
            return ""
        imstr = np.fromstring(imstrjpg, dtype=np.uint8)
        im = cv2.imdecode(imstr, cv2.CV_LOAD_IMAGE_UNCHANGED)
        scores, boxes = detector.detect(im)
        CONF_THRESH = 0.15
        NMS_THRESH = 0.08
        results = {}
        for cls_ind, cls in enumerate(CLASSES[1:]):
            cls_ind += 1 # because we skipped background
            cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
            cls_scores = scores[:, cls_ind]
            dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
            keep = nms(dets, NMS_THRESH)
            dets = dets[keep, :]
            results[cls] = dets

        self.write(cPickle.dumps(results))
        self.finish()
项目:opencv-api    作者:last-stand    | 项目源码 | 文件源码
def _grab_image(path=None, stream=None, url=None):
    # if the path is not None, then load the image from disk
    if path is not None:
        image = cv2.imread(path)

    # otherwise, the image does not reside on disk
    else:
        # if the URL is not None, then download the image
        if url is not None:
            resp = urllib.urlopen(url)
            data = resp.read()

        # if the stream is not None, then the image has been uploaded
        elif stream is not None:
            data = stream.read()

        # convert the image to a NumPy array and then read it into
        # OpenCV format
        image = np.asarray(bytearray(data), dtype="uint8")
        image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    # return the image
    return image
项目:Cuppa    作者:flipkart-incubator    | 项目源码 | 文件源码
def __caffe_predict(self, net, height, width, url):
        # logger = logging.getLogger(__name__)
        #
        # logger.info("caffe_predict has been called")

        input_layer = net.inputs[0]
        output_layer = net.outputs[0]
        r = requests.get(url, allow_redirects=False)
        arr = numpy.asarray(bytearray(r.content), dtype=numpy.uint8)
        img = cv2.imdecode(arr, -1)
        resized_img = imresize(img, (height,width), 'bilinear')
        transposed_resized_img = numpy.transpose(resized_img, (2,0,1))
        reqd_shape = (1,) + transposed_resized_img.shape
        #net.blobs["data_q"].reshape(*reqd_shape)
        #net.blobs["data_q"].data[...] = transposed_resized_img
        net.blobs[input_layer].reshape(*reqd_shape)
        net.blobs[input_layer].data[...] = transposed_resized_img
        net.forward()
        #result = net.blobs['latent_q_encode'].data[0].tolist()
        result = net.blobs[output_layer].data[0].tolist()
        return result
项目:didi-competition    作者:udacity    | 项目源码 | 文件源码
def write_image(self, outdir, msg, fmt='png'):
        results = {}
        image_filename = os.path.join(outdir, str(msg.header.stamp.to_nsec()) + '.' + fmt)
        try:
            if hasattr(msg, 'format') and 'compressed' in msg.format:
                buf = np.ndarray(shape=(1, len(msg.data)), dtype=np.uint8, buffer=msg.data)
                cv_image = cv2.imdecode(buf, cv2.IMREAD_ANYCOLOR)
                if cv_image.shape[2] != 3:
                    print("Invalid image %s" % image_filename)
                    return results
                results['height'] = cv_image.shape[0]
                results['width'] = cv_image.shape[1]
                # Avoid re-encoding if we don't have to
                if check_image_format(msg.data) == fmt:
                    buf.tofile(image_filename)
                else:
                    cv2.imwrite(image_filename, cv_image)
            else:
                cv_image = self.bridge.imgmsg_to_cv2(msg, "bgr8")
                cv2.imwrite(image_filename, cv_image)
        except CvBridgeError as e:
            print(e)
        results['filename'] = image_filename
        return results
项目:mxnet_tk1    作者:starimpact    | 项目源码 | 文件源码
def unpack_img(s, iscolor=-1):
    """unpack a MXImageRecord to image

    Parameters
    ----------
    s : str
        string buffer from MXRecordIO.read
    iscolor : int
        image format option for cv2.imdecode

    Returns
    -------
    header : IRHeader
        header of the image record
    img : numpy.ndarray
        unpacked image
    """
    header, s = unpack(s)
    img = np.fromstring(s, dtype=np.uint8)
    assert opencv_available
    img = cv2.imdecode(img, iscolor)
    return header, img
项目:mxnet_tk1    作者:starimpact    | 项目源码 | 文件源码
def imdecode(str_img, flag=1):
    """Decode image from str buffer.
    Wrapper for cv2.imdecode that uses mx.nd.NDArray

    Parameters
    ----------
    str_img : str
        str buffer read from image file
    flag : int
        same as flag for cv2.imdecode
    Returns
    -------
    img : NDArray
        decoded image in (width, height, channels)
        with BGR color channel order
    """
    hdl = NDArrayHandle()
    check_call(_LIB.MXCVImdecode(ctypes.c_char_p(str_img),
                                 mx_uint(len(str_img)),
                                 flag, ctypes.byref(hdl)))
    return mx.nd.NDArray(hdl)
项目:siam    作者:btlk    | 项目源码 | 文件源码
def read_labeled_data(images_dir, labels_file):
  images_data = []
  labels_list = [int(x.strip()) 
    for x in open(labels_file, 'r').readlines()]

  images_list = sorted(os.listdir(images_dir))
  for im in images_list:
    with open(os.path.join(
      images_dir, im), 'rb') as img_stream:
      file_bytes = np.asarray(
        bytearray(img_stream.read()), dtype=np.uint8)
      img_data_ndarray = cv2.imdecode(
        file_bytes, cv2.IMREAD_UNCHANGED)
      images_data.append(img_data_ndarray)

  return np.asarray(images_data), \
    np.asarray(labels_list)
项目:siam    作者:btlk    | 项目源码 | 文件源码
def read_labeled_data2(images_dir):
  dirs_list = os.listdir(images_dir)

  images_data = []
  labels_list = []

  for d in dirs_list:
    images_list = os.listdir(
      os.path.join(images_dir, d))
    for im in images_list:
      with open(os.path.join(
        images_dir, d, im), 'rb') as img_stream:
        file_bytes = np.asarray(
          bytearray(img_stream.read()), dtype=np.uint8)
        img_data_ndarray = cv2.imdecode(
          file_bytes, cv2.IMREAD_UNCHANGED)

        images_data.append(img_data_ndarray)
        labels_list.append(int(d))

  return np.asarray(images_data), \
    np.asarray(labels_list)
项目:face-recognition-demo    作者:SaMnCo    | 项目源码 | 文件源码
def _grab_image(path=None, stream=None, url=None):
    # if the path is not None, then load the image from disk
    if path is not None:
        image = cv2.imread(path)

    # otherwise, the image does not reside on disk
    else:   
        # if the URL is not None, then download the image
        if url is not None:
            resp = urllib.urlopen(url)
            data = resp.read()

        # if the stream is not None, then the image has been uploaded
        elif stream is not None:
            data = stream.read()

        # convert the image to a NumPy array and then read it into
        # OpenCV format
        image = np.asarray(bytearray(data), dtype="uint8")
        image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    # return the image
    return image
项目:pytorch-playground    作者:aaron-xichen    | 项目源码 | 文件源码
def load_lmdb(lmdb_file, n_records=None):
    import lmdb
    import numpy as np
    lmdb_file = expand_user(lmdb_file)
    if os.path.exists(lmdb_file):
        data = []
        env = lmdb.open(lmdb_file, readonly=True, max_readers=512)
        with env.begin() as txn:
            cursor = txn.cursor()
            begin_st = time.time()
            print("Loading lmdb file {} into memory".format(lmdb_file))
            for key, value in cursor:
                _, target, _ = key.decode('ascii').split(':')
                target = int(target)
                img = cv2.imdecode(np.fromstring(value, np.uint8), cv2.IMREAD_COLOR)
                data.append((img, target))
                if n_records is not None and len(data) >= n_records:
                    break
        env.close()
        print("=> Done ({:.4f} s)".format(time.time() - begin_st))
        return data
    else:
        print("Not found lmdb file".format(lmdb_file))
项目:Vehicle_ReID    作者:starimpact    | 项目源码 | 文件源码
def unpack_img(s, iscolor=-1):
    """unpack a MXImageRecord to image

    Parameters
    ----------
    s : str
        string buffer from MXRecordIO.read
    iscolor : int
        image format option for cv2.imdecode

    Returns
    -------
    header : IRHeader
        header of the image record
    img : numpy.ndarray
        unpacked image
    """
    header, s = unpack(s)
    img = np.fromstring(s, dtype=np.uint8)
    assert opencv_available
    img = cv2.imdecode(img, iscolor)
    return header, img
项目:async_face_recognition    作者:dpdornseifer    | 项目源码 | 文件源码
def _cascade_detect(self, raw_image):
        ''' use opencv cascades to recognize objects on the incomming images '''
        cascade = cv2.CascadeClassifier(self._cascade)
        image = np.asarray(bytearray(raw_image), dtype="uint8")

        gray_image = cv2.imdecode(image, cv2.IMREAD_GRAYSCALE)
        color_image = cv2.imdecode(image, cv2.IMREAD_ANYCOLOR)

        coordinates = cascade.detectMultiScale(
            gray_image,
            scaleFactor=1.15,
            minNeighbors=5,
            minSize=(30, 30)
        )

        for (x, y, w, h) in coordinates:
            cv2.rectangle(color_image, (x, y), (x + w, y + h), (0, 255, 0), 2)
            self._logger.debug("face recognized at: x: {}, y: {}, w: {}, h: {}".format(x, y, w, h))

        return color_image, self._tojson(coordinates)
项目:face-classifier-cnn    作者:nknytk    | 项目源码 | 文件源码
def load_img(file_path):
    try:
        if os.path.exists(file_path):
            return cv2.imread(file_path)

        elif file_path.startswith('http'):
            with urlopen(file_path) as fp:
                img_bin = numpy.fromstring(fp.read(), dtype=numpy.uint8)
                mime = fp.getheader('Content-Type', '')
                print(mime)
            if MIME_JPG_PTN.match(mime):
                return cv2.imdecode(img_bin, cv2.IMREAD_UNCHANGED)
            elif MIME_PNG_PTN.match(mime):
                return cv2.imdecode(img_bin, cv2.IMREAD_UNCHANDED)
            else:
                sys.stderr.write('Unacceptable mime type {}.\n'.format(mime))

        else:
            sys.stderr.write('{} is not found.\n'.format(file_path))

    except Exception as e:
        sys.stderr.write('Failed to load {} by {}\n'.format(file_path, e))

    return None
项目:crnn    作者:wulivicte    | 项目源码 | 文件源码
def checkImageIsValid(imageBin):
    if imageBin is None:
        return False
    try:
        imageBuf = np.fromstring(imageBin, dtype=np.uint8)
        img = cv2.imdecode(imageBuf, cv2.IMREAD_GRAYSCALE)
        imgH, imgW = img.shape[0], img.shape[1]
    except:
        return False
    else:
        if imgH * imgW == 0:
            return False        
    return True
项目:moVi    作者:netsecIITK    | 项目源码 | 文件源码
def decode(self, byte_list):
        return cv2.imdecode(byte_list, cv2.IMREAD_COLOR)
项目:pybot    作者:spillai    | 项目源码 | 文件源码
def decode(self, data): 
        msg = image_t.decode(data)
        if msg.pixelformat == image_t.PIXEL_FORMAT_GRAY: 
            return im_resize(np.asarray(bytearray(msg.data), dtype=np.uint8).reshape(msg.height, msg.width), scale=self.scale)
        elif msg.pixelformat == image_t.PIXEL_FORMAT_MJPEG: 
            im = cv2.imdecode(np.asarray(bytearray(msg.data), dtype=np.uint8), -1)
            return im_resize(im, scale=self.scale)
        else: 
            raise RuntimeError('Unknown pixelformat for ImageDecoder')
项目:pybot    作者:spillai    | 项目源码 | 文件源码
def decode_rgb(self, data): 
        w, h = data.image.width, data.image.height;
        if data.image.image_data_format == self.image_msg_t_.VIDEO_RGB_JPEG: 
            img = cv2.imdecode(np.asarray(bytearray(data.image.image_data), dtype=np.uint8), -1)
            bgr = img.reshape((h,w,3))[::self.skip, ::self.skip, :]             
        else: 
            img = np.fromstring(data.image.image_data, dtype=np.uint8)
            rgb = img.reshape((h,w,3))[::self.skip, ::self.skip, :] 
            bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)
        if not self.bgr: 
            return cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)
        else: 
            return bgr
项目:pybot    作者:spillai    | 项目源码 | 文件源码
def compressed_imgmsg_to_cv2(cmprs_img_msg, desired_encoding = "passthrough"):
    """
    Convert a sensor_msgs::CompressedImage message to an OpenCV :cpp:type:`cv::Mat`.

    :param cmprs_img_msg:   A :cpp:type:`sensor_msgs::CompressedImage` message
    :param desired_encoding:  The encoding of the image data, one of the following strings:

       * ``"passthrough"``
       * one of the standard strings in sensor_msgs/image_encodings.h

    :rtype: :cpp:type:`cv::Mat`
    :raises CvBridgeError: when conversion is not possible.

    If desired_encoding is ``"passthrough"``, then the returned image has the same format as img_msg.
    Otherwise desired_encoding must be one of the standard image encodings

    This function returns an OpenCV :cpp:type:`cv::Mat` message on success, or raises :exc:`cv_bridge.CvBridgeError` on failure.

    If the image only has one channel, the shape has size 2 (width and height)
    """
    str_msg = cmprs_img_msg.data
    buf = np.ndarray(shape=(1, len(str_msg)),
                      dtype=np.uint8, buffer=cmprs_img_msg.data)
    im = cv2.imdecode(buf, cv2.IMREAD_ANYCOLOR)

    if desired_encoding == "passthrough":
        return im

    try:
        res = cvtColor2(im, "bgr8", desired_encoding)
    except RuntimeError as e:
        raise CvBridgeError(e)

    return res
项目:pybot    作者:spillai    | 项目源码 | 文件源码
def read_image(conn): 
    try: 
        length = int(recvall(conn, 16))
    except:
        import sys
        print "Unexpected error:", sys.exc_info()[0]
        return False, None

    stringData = recvall(conn, length)
    data = np.fromstring(stringData, dtype='uint8')
    decimg = cv2.imdecode(data, 1)
    print 'Image received ', decimg.shape
    return True, decimg
项目:pokedex-as-it-should-be    作者:leotok    | 项目源码 | 文件源码
def predict_knn(image_file):
    image = cv2.imdecode(np.fromstring(image_file.read(), np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED)
    if image is not None:
        features = np.array([extract_color_histogram(image)])
        loaded_model = pickle.load(open(MODEL_PATH + "/knn_model.sav", 'rb'))

        return loaded_model.predict(features)[0]
    else:
        raise "Failed"
项目:pokedex-as-it-should-be    作者:leotok    | 项目源码 | 文件源码
def predict_mlp(image_file):
    image = cv2.imdecode(np.fromstring(image_file.read(), np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED)
    if image is not None:
        features = np.array([image_to_feature_vector(image)])
        loaded_model = pickle.load(open(MODEL_PATH + "/mlp_model.sav", 'rb'))
        scaler = pickle.load(open(MODEL_PATH + "/scaler_model.sav", "rb"))
        features = scaler.transform(features)

        return loaded_model.predict(features)[0]
    else:
        raise "Failed"
项目:FaceSwapper    作者:QuantumLiu    | 项目源码 | 文件源码
def read_im(self,fname,scale=1):
        '''
        ????
        '''
# =============================================================================
#         im = cv2.imread(fname, cv2.IMREAD_COLOR)
# =============================================================================
        im = cv2.imdecode(np.fromfile(fname,dtype=np.uint8),-1)
        if type(im)==type(None):
            print(fname)
            raise ValueError('Opencv read image {} error, got None'.format(fname))
        return im
项目:camera_calibration_frontend    作者:groundmelon    | 项目源码 | 文件源码
def image_from_archive(archive, name):
    """
    Load image PGM file from tar archive. 

    Used for tarfile loading and unit test.
    """
    member = archive.getmember(name)
    imagefiledata = numpy.fromstring(archive.extractfile(member).read(-1), numpy.uint8)
    imagefiledata.resize((1, imagefiledata.size))
    return cv2.imdecode(imagefiledata, cv2.IMREAD_COLOR)
项目:soja_box    作者:iTaa    | 项目源码 | 文件源码
def cv_test(original_image_name):
    with open(original_image_name) as f:
        img = cv2.imdecode(f, 1)
        img2 = cv2.imread(original_image_name)
        if img == img2:
            print("yes you are right")
        else:
            print("can not do this")
项目:MIL.pytorch    作者:gujiuxiang    | 项目源码 | 文件源码
def url_to_image(url):
    # download the image, convert it to a NumPy array, and then read
    # it into OpenCV format
    resp = urllib.urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    # return the image
    return image
项目:Emotion-Recognition    作者:Shujathlive    | 项目源码 | 文件源码
def format_image(image):
  if len(image.shape) > 2 and image.shape[2] == 3:
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  else:
    image = cv2.imdecode(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
  faces = cascade_classifier.detectMultiScale(
      image,
      scaleFactor = 1.3,
      minNeighbors = 5
  )
  # None is we don't found an image
  if not len(faces) > 0:
    return None
  max_area_face = faces[0]
  for face in faces:
    if face[2] * face[3] > max_area_face[2] * max_area_face[3]:
      max_area_face = face
  # Chop image to face
  face = max_area_face
  image = image[face[1]:(face[1] + face[2]), face[0]:(face[0] + face[3])]
  # Resize image to network size
  try:
    image = cv2.resize(image, (SIZE_FACE, SIZE_FACE), interpolation = cv2.INTER_CUBIC) / 255.
  except Exception:
    print("[+] Problem during resize")
    return None
  # cv2.imshow("Lol", image)
  # cv2.waitKey(0)
  return image

# Load Model
项目:python-streaming-server    作者:golubaca    | 项目源码 | 文件源码
def rcv():
    data = b''
    while 1:

        try:
            r = client_socket.recv(90456)
            if len(r) == 0:
                exit(0)
            a = r.find(b'END!')
            if a != -1:
                data += r[:a]
                break
            data += r
        except Exception as e:
            print(e)
            continue
    nparr = numpy.fromstring(data, numpy.uint8)
    frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    if type(frame) is type(None):
        pass
    else:
        try:
            cv2.imshow(name,frame)
            if cv2.waitKey(10) == ord('q'):
                client_socket.close()
                sys.exit()
        except:
            client_socket.close()
            exit(0)
项目:ATX    作者:NetEaseGame    | 项目源码 | 文件源码
def _open_data_url(data, flag=cv2.IMREAD_COLOR):
    pos = data.find('base64,')
    if pos == -1:
        raise IOError("data url is invalid, head %s" % data[:20])

    pos += len('base64,')
    raw_data = base64.decodestring(data[pos:])
    image = np.asarray(bytearray(raw_data), dtype="uint8")
    image = cv2.imdecode(image, flag)
    return image
项目:ATX    作者:NetEaseGame    | 项目源码 | 文件源码
def url_to_image(url, flag=cv2.IMREAD_COLOR):
    """ download the image, convert it to a NumPy array, and then read
    it into OpenCV format """
    resp = urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, flag)
    return image
项目:ATX    作者:NetEaseGame    | 项目源码 | 文件源码
def str2img(jpgstr, orientation=None):
    import numpy as np
    import cv2
    arr = np.fromstring(jpgstr, np.uint8)
    img = cv2.imdecode(arr, cv2.IMREAD_COLOR)
    if orientation == 1:
        return cv2.flip(cv2.transpose(img), 0) # counter-clockwise
    if orientation == 3:
        return cv2.flip(cv2.transpose(img), 1) # clockwise
    return img
项目:image-segmentation    作者:alexlouden    | 项目源码 | 文件源码
def download_image(url):
    response = requests.get(url, stream=True, timeout=5)
    # TODO use grequests
    # Raise exception on error
    response.raise_for_status()
    numpy_array = np.asarray(bytearray(response.raw.read()), dtype=np.uint8)
    image = cv2.imdecode(numpy_array, cv2.IMREAD_COLOR)
    # TODO: handle transparency (load using cv2.IMREAD_UNCHANGED and convert alpha layer to white?)
    return image
项目:Yugioh-bot    作者:will7200    | 项目源码 | 文件源码
def get_img_from_screen_shot(self):
        screen_shot = self.take_png_screenshot()
        nparr = np.fromstring(screen_shot, np.uint8)
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        return img
项目:StreamMotionDetection    作者:henry54809    | 项目源码 | 文件源码
def get_mjpeg_stream_image(stream):
    global bytes
    while True:
        bytes += stream.read(20000)
        a = bytes.find(b'\xff\xd8')
        b = bytes.find(b'\xff\xd9')
        if a!= -1 and b != -1:
             jpg = bytes[a:b+2]
             bytes = bytes[b+2:]
             img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
             q.put_nowait(img)
        cv2.waitKey(1)