def image_to_file(image):
f = StringIO()
ret, buf = cv2.imencode('.jpg', image)
f.write(np.array(buf).tostring())
f.seek(0)
return f
python类imencode()的实例源码
def videoToFrame(videoName):
print(videoName)
cap = cv2.VideoCapture(videoName)
flag, frame = cap.read()
global frameCnt
skipCnt = 0
width, height = np.shape(frame)[1], np.shape(frame)[0]
while flag:
for i in range(segXNum):
for j in range(segYNum):
frame2 = frame[j * height // segYNum : (j + 1) * height // segYNum,
i * width // segXNum : (i + 1) * width // segXNum] #get ROI
#????????????
cv2.imencode('.jpg', frame2)[1].tofile("E:\\traffic\\?????\\???\\image\\" + '{:0>6}'.format(str(frameCnt + 1)) + '.jpg')
#cv2.imwrite("E:\\traffic\\?????\\???\\image\\" + '{:0>6}'.format(str(frameCnt // skipFrameNum + 1)) + '.jpg', frame)
frameCnt += 1
skipCnt += 1
cap.set(1, skipCnt * skipFrameNum)
flag, frame = cap.read()
return
def resizing(img):
height, width, channels = img.shape
if max(height, width) > 100:
ratio = float(height) / width
new_width = 100 / ratio
img_resized = cv2.resize(img, (int(new_width), 100))
ip_convert = cv2.imencode('.png', img_resized)
else:
ip_convert = cv2.imencode('.png', img)
return ip_convert
def removebg(segmented_img):
src = cv2.imdecode(np.squeeze(np.asarray(segmented_img[1])), 1)
tmp = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
_, alpha = cv2.threshold(tmp, 0, 255, cv2.THRESH_BINARY)
b, g, r = cv2.split(src)
rgba = [b, g, r, alpha]
dst = cv2.merge(rgba, 4)
processed_img = cv2.imencode('.png', dst)
return processed_img
def to_pillow(image):
return Image.fromarray(image[:, :, ::-1].copy())
# There is another way
# img_bytes = cv2.imencode('.png', image)[1].tostring()
# return Image.open(StringIO(img_bytes))
def save_inp_image(self, img, group):
img_str = cv2.imencode('.png', img)[1]
self.save('input', img_str, group)
def save_full_image(self, img, group):
img_str = cv2.imencode('.png', img)[1]
self.save('input_full_size', img_str, group)
def save_seg(self, seg_id, seg, group):
seg_str = cv2.imencode('.png', seg)[1]
key = 'label_segmentation/{:02d}'.format(seg_id)
self.save(key, seg_str, group)
def save_ori(self, ori, group):
ori_str = cv2.imencode('.png', ori)[1]
self.save('orientation', ori_str, group)
def save_full_seg(self, seg_id, seg, group):
seg_str = cv2.imencode('.png', seg)[1]
key = 'label_segmentation_full_size/{:02d}'.format(seg_id)
self.save(key, seg_str, group)
def save_full_sem_seg(self, cls_id, seg, group):
seg_str = cv2.imencode('.png', seg)[1]
key = 'label_semantic_segmentation_full_size/{:02d}'.format(cls_id)
self.save(key, seg_str, group)
def write_log(self, results):
"""Process results
Args:
results: y_out, s_out
"""
inp = self._batch
y_out = results['y_out']
d_out = results['d_out']
with h5py.File(self.dataset.h5_fname, 'r+') as h5f:
for ii in xrange(y_out.shape[0]):
idx = inp['idx_map'][ii]
group = h5f[self.dataset.get_str_id(idx)]
if 'foreground_pred' in group:
del group['foreground_pred']
if 'orientation_pred' in group:
del group['orientation_pred']
for cl in range(y_out.shape[3]):
y_out_arr = y_out[ii, :, :, cl]
y_out_arr = (y_out_arr * 255).astype('uint8')
y_out_str = cv2.imencode('.png', y_out_arr)[1]
group['foreground_pred/{:02d}'.format(cl)] = y_out_str
for ch in range(d_out.shape[3]):
d_out_arr = d_out[ii, :, :, ch]
d_out_arr = (d_out_arr * 255).astype('uint8')
d_out_str = cv2.imencode('.png', d_out_arr)[1]
group['orientation_pred/{:02d}'.format(ch)] = d_out_str
def _captureSingleImage(self):
Return, Frame = self._VideoStream.read()
FrameImage = cv2.cvtColor(Frame, cv2.COLOR_BGR2RGBA)
FrameImage = cv2.resize(FrameImage, self._getLabelSize(self._VideoLabel, 480/320), interpolation = cv2.INTER_CUBIC)
ArrayImage = image.fromarray(FrameImage)
#ArrayImage = ArrayImage.resize(, image.ANTIALIAS)
Image = imagetk.PhotoImage(image=ArrayImage)
self._VideoLabel.imgtk = Image
self._VideoLabel.configure(image=Image)
#cv2.imencode(".jpg", Frame)
return(cv2.imencode('.jpg', Frame)[1].tostring())
def save_inp_image(self, img, group):
img_str = cv2.imencode(".png", img)[1]
self.save("input", img_str, group)
pass
def save_full_image(self, img, group):
img_str = cv2.imencode(".png", img)[1]
self.save("input_full", img_str, group)
def save_ori(self, ori, group):
ori_str = cv2.imencode(".png", ori)[1]
self.save("label_angle", ori_str, group)
pass
def save_full_seg(self, seg_id, seg, group):
seg_str = cv2.imencode(".png", seg)[1]
key = "label_ins_seg_full/{:03d}".format(seg_id)
self.save(key, seg_str, group)
pass
def save_full_sem_seg(self, cls_id, seg, group):
seg_str = cv2.imencode(".png", seg)[1]
key = "label_sem_seg_full/{:03d}".format(cls_id)
self.save(key, seg_str, group)
pass
def current_img_b64():
b64 = None
frame = current_img()
if frame != None:
png = cv2.imencode('.png', frame)[1]
b64 = base64.encodestring(png)
return b64
# GET /see
# returns the current image
def _convert_jpg(self, image_msg):
retval, buf = imencode('.jpg', image_msg)
return buf.tostring()