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
python类imdecode()的实例源码
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()
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)
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
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()
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]
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
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
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}
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}
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}
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}
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
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
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
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')
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()
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()
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
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
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
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
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)
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)
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)
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
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))
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
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)
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