def draw_all_detection(im_array, detections, class_names, scale, cfg, threshold=1e-1):
"""
visualize all detections in one image
:param im_array: [b=1 c h w] in rgb
:param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
:param class_names: list of names in imdb
:param scale: visualize the scaled image
:return:
"""
import cv2
import random
color_white = (255, 255, 255)
im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
# change to bgr
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
for j, name in enumerate(class_names):
if name == '__background__':
continue
color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)) # generate a random color
dets = detections[j]
for det in dets:
bbox = det[:4] * scale
score = det[-1]
if score < threshold:
continue
bbox = map(int, bbox)
cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=color, thickness=2)
cv2.putText(im, '%s %.3f' % (class_names[j], score), (bbox[0], bbox[1] + 10),
color=color_white, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5)
return im
python类COLOR_RGB2BGR的实例源码
def draw_all_detection(im_array, detections, class_names, scale, cfg, threshold=1e-1):
"""
visualize all detections in one image
:param im_array: [b=1 c h w] in rgb
:param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
:param class_names: list of names in imdb
:param scale: visualize the scaled image
:return:
"""
import cv2
import random
color_white = (255, 255, 255)
im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
# change to bgr
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
for j, name in enumerate(class_names):
if name == '__background__':
continue
color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)) # generate a random color
dets = detections[j]
for det in dets:
bbox = det[:4] * scale
score = det[-1]
if score < threshold:
continue
bbox = map(int, bbox)
cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=color, thickness=2)
cv2.putText(im, '%s %.3f' % (class_names[j], score), (bbox[0], bbox[1] + 10),
color=color_white, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5)
return im
def draw_all_detection(im_array, detections, class_names, scale, cfg, threshold=1e-1):
"""
visualize all detections in one image
:param im_array: [b=1 c h w] in rgb
:param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
:param class_names: list of names in imdb
:param scale: visualize the scaled image
:return:
"""
import cv2
import random
color_white = (255, 255, 255)
im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
# change to bgr
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
for j, name in enumerate(class_names):
if name == '__background__':
continue
color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)) # generate a random color
dets = detections[j]
for det in dets:
bbox = det[:4] * scale
score = det[-1]
if score < threshold:
continue
bbox = map(int, bbox)
cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=color, thickness=2)
cv2.putText(im, '%s %.3f' % (class_names[j], score), (bbox[0], bbox[1] + 10),
color=color_white, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5)
return im
def draw_all_detection(im_array, detections, class_names, scale, cfg, threshold=1e-1):
"""
visualize all detections in one image
:param im_array: [b=1 c h w] in rgb
:param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
:param class_names: list of names in imdb
:param scale: visualize the scaled image
:return:
"""
import cv2
import random
color_white = (255, 255, 255)
im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
# change to bgr
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
for j, name in enumerate(class_names):
if name == '__background__':
continue
color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)) # generate a random color
dets = detections[j]
for det in dets:
bbox = det[:4] * scale
score = det[-1]
if score < threshold:
continue
bbox = map(int, bbox)
cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=color, thickness=2)
cv2.putText(im, '%s %.3f' % (class_names[j], score), (bbox[0], bbox[1] + 10),
color=color_white, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5)
return im
def Observation(self):
obs = self.env.observations()
img = obs["RGB_INTERLACED"]
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
return img
def imread_from_base64(base64_str):
sbuf = StringIO()
sbuf.write(base64.b64decode(base64_str))
pimg = Image.open(sbuf)
return cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)
def exportCV2(self):
'''
Use cv2.imwrite() to save the image array
'''
w = self.display.widget
def fn(path, img):
r = self.pRange.value()
if r == '0-max':
r = (0, w.levelMax)
elif r == 'min-max':
r = (w.levelMin, w.levelMax)
else: # 'current'
r = w.ui.histogram.getLevels()
int_img = toUIntArray(img,
# cutNegative=self.pCutNegativeValues.value(),
cutHigh=~self.pStretchValues.value(),
range=r,
dtype={'8 bit': np.uint8,
'16 bit': np.uint16}[
self.pDType.value()])
if isColor(int_img):
int_img = cv2.cvtColor(int_img, cv2.COLOR_RGB2BGR)
cv2.imwrite(path, int_img)
return self._export(fn)
def draw_all_detection(im, detections, class_names, scale = 1.0):
"""
visualize all detections in one image
:param im_array: [b=1 c h w] in rgb
:param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
:param class_names: list of names in imdb
:param scale: visualize the scaled image
:return:
"""
import cv2
import random
color_white = (255, 255, 255)
# im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
# change to bgr
#im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
for j, name in enumerate(class_names):
if name == '__background__':
continue
color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)) # generate a random color
dets = detections[j]
for det in dets:
bbox = det[:4] * scale
score = det[-1]
# if score < threshold:
# continue
bbox = map(int, bbox)
cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=color, thickness=2)
cv2.putText(im, '%s %.3f' % (class_names[j], score), (bbox[0], bbox[1] + 10),
color=color_white, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5)
return im
def observe(self):
if self.show is True:
cv2.imshow("show", cv2.cvtColor(self.env.getScreenRGB(),cv2.COLOR_RGB2BGR))
cv2.waitKey(self.delay)
return cv2.resize(self.env.getScreenGrayscale(), (self.width, self.height), interpolation=cv2.INTER_LINEAR)
# return (cv2.resize(cv2.cvtColor(self.env.getScreenRGB(),cv2.COLOR_BGR2YUV)[:,:,0], (self.width, self.height) , interpolation=cv2.INTER_LINEAR)) #/ np.float32(255)
def make_thumbnail(video, db):
indices = [int(n * video.num_frames) for n in [0.1, 0.35, 0.60, 0.85]]
table = db.table(video.path)
frames = [f[0] for _, f in table.load([1], rows=indices)]
img = make_montage(len(frames), iter(frames), frame_width=150, frames_per_row=2)
run('mkdir -p assets/thumbnails')
cv2.imwrite('assets/thumbnails/{}.jpg'.format(video.id), cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
def execute(self, columns):
global i
[img, bboxes] = columns
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
[h, w] = img.shape[:2]
bboxes = parsers.bboxes(bboxes, self.protobufs)
imgs = [img[int(h*bbox.y1):int(h*bbox.y2), int(w*bbox.x1):int(w*bbox.x2)]
for bbox in bboxes]
for img in imgs:
cv2.imwrite('/app/tmp/{:05d}.jpg'.format(i), img)
i += 1
genders = self.rc.get_gender_batch(imgs)
outputs = [struct.pack('=cf', label, score) for [label, score] in genders]
assert(len(outputs) == len(imgs))
return [''.join(outputs)]
def faceCrop(targetDir, imgList, color, single_face):
# Load list of Haar cascades for faces
faceCascades = load_cascades()
# Iterate through images
face_list = []
for img in imgList:
if os.path.isdir(img):
continue
pil_img = Image.open(img)
if color:
cv_img = cv.cvtColor(np.array(pil_img), cv.COLOR_RGB2BGR)
else:
cv_img = np.array(pil_img)
# Convert to grayscale if this image is actually color
if cv_img.ndim == 3:
cv_img = cv.cvtColor(np.array(pil_img), cv.COLOR_BGR2GRAY)
# Detect all faces in this image
scaled_img, faces = DetectFace(cv_img, color, faceCascades, single_face, second_pass=False, draw_rects=False)
# Iterate through faces
n=1
for face in faces:
cropped_cv_img = imgCrop(scaled_img, face, scale=1.0)
if color:
cropped_cv_img = rgb(cropped_cv_img)
fname, ext = os.path.splitext(img)
cropped_pil_img = Image.fromarray(cropped_cv_img)
#save_name = loc + '/cropped/' + fname.split('/')[-1] + '_crop' + str(n) + ext
save_name = targetDir + '/' + fname.split('/')[-1] + '_crop' + str(n) + ext
cropped_pil_img.save(save_name)
face_list.append(save_name)
n += 1
return face_list
# Add an emoji to an image at a specified point and size
# Inputs: img, emoji are ndarrays of WxHx3
# faces is a list of (x,y,w,h) tuples for each face to be replaced
def imwrite(path, img):
"""Wrapper around cv2.imwrite. Switches it to RGB input convention.
:param path:
String indicating path to save image to.
:param img:
3D RGB numpy array of image.
"""
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite(path, img)
def convert_to_opencv(img):
return cv2.cvtColor(numpy.array(img.convert('RGB')), cv2.COLOR_RGB2BGR)
def main():
# actions are translation and angular speed (angular velocity constraint to the (0, 0, 1) axis)
action_space = TranslationAxisAngleSpace(low=[-10, -10, -10, -np.pi/4],
high=[10, 10, 10, np.pi/4],
axis=[0, 0, 1])
env = SimpleQuadPanda3dEnv(action_space, sensor_names=['image', 'depth_image'])
num_trajs = 10
num_steps = 100
done = False
for traj_iter in range(num_trajs):
env.reset()
for step_iter in range(num_steps):
action = action_space.sample()
obs, _, _, _ = env.step(action)
image, depth_image = obs['image'], obs['depth_image']
# convert BGR image to RGB image
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imshow("image", image)
# rescale depth image to be between 0 and 255
depth_scale = depth_image.max() - depth_image.min()
depth_offset = depth_image.min()
depth_image = np.clip((depth_image - depth_offset) / depth_scale, 0.0, 1.0)
depth_image = (255.0 * depth_image).astype(np.uint8)
cv2.imshow("depth image", depth_image)
env.render()
key = cv2.waitKey(10)
key &= 255
if key == 27 or key == ord('q'):
print("Pressed ESC or q, exiting")
done = True
if done:
break
if done:
break
def draw_matches(I, boxes, matches, anns):
I = np.copy(I) * 255.0
for o in range(len(layer_boxes)):
for y in range(c.out_shapes[o][2]):
for x in range(c.out_shapes[o][1]):
for i in range(layer_boxes[o]):
match = matches[o][x][y][i]
# None if not positive nor negative
# -1 if negative
# ground truth indices if positive
if match == -1:
coords = center2cornerbox(boxes[o][x][y][i])
draw_rect(I, coords, (255, 0, 0))
elif isinstance(match, tuple):
coords = center2cornerbox(boxes[o][x][y][i])
draw_rect(I, coords, (0, 0, 255))
# elif s == 2:
# draw_rect(I, boxes[o][x][y][i], (0, 0, 255), 2)
for gt_box, id in anns:
draw_rect(I, gt_box, (0, 255, 0), 3)
cv2.putText(I, i2name[id], (int(gt_box[0] * image_size), int((gt_box[1] + gt_box[3]) * image_size)),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))
I = cv2.cvtColor(I.astype(np.uint8), cv2.COLOR_RGB2BGR)
cv2.imshow("matches", I)
cv2.waitKey(1)
def draw_matches2(I, pos, neg, true_labels, true_locs):
I = np.copy(I) * 255.0
index = 0
for o in range(len(layer_boxes)):
for y in range(c.out_shapes[o][2]):
for x in range(c.out_shapes[o][1]):
for i in range(layer_boxes[o]):
if pos[index] > 0:
d = c.defaults[o][x][y][i]
coords = default2cornerbox(d, true_locs[index])
draw_rect(I, coords, (0, 255, 0))
coords = center2cornerbox(d)
draw_rect(I, coords, (0, 0, 255))
cv2.putText(I, i2name[true_labels[index]],
(int(coords[0] * image_size), int((coords[1] + coords[3]) * image_size)),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255))
elif neg[index] > 0:
pass
#d = defaults[o][x][y][i]
#coords = default2global(d, pred_locs[index])
#draw_rect(I, coords, (255, 0, 0))
#cv2.putText(I, coco.i2name[true_labels[index]],
# (int(coords[0] * image_size), int((coords[1] + coords[3]) * image_size)),
# cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0))
index += 1
I = cv2.cvtColor(I.astype(np.uint8), cv2.COLOR_RGB2BGR)
cv2.imshow("matches2", I)
cv2.waitKey(1)
def saveImg_function(self, arg_frame,arg_savePath, arg_filename):
utils_tool.check_path(arg_savePath)
# make sure output dir exists
#if(not path.isdir(arg_savePath)):
# makedirs(arg_savePath)
#tmp= cv2.cvtColor(arg_frame, cv2.COLOR_RGB2BGR)
cv2.imwrite(arg_savePath+arg_filename+'.jpg',arg_frame)
def read_base64(base64_string):
"""read an image from base64 string"""
sbuf = BytesIO()
sbuf.write(base64.b64decode(base64_string))
pimg = Image.open(sbuf)
return cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)
def train():
"""
Train both generator and discriminator
:return:
"""
# Load data
print 'Loading training data...'
with open('../saliency-2016-lsun/validationSample240x320.pkl', 'rb') as f:
# with open(TRAIN_DATA_DIR, 'rb') as f:
train_data = pickle.load(f)
print '-->done!'
print 'Loading validation data...'
with open('../saliency-2016-lsun/validationSample240x320.pkl', 'rb') as f:
# with open(VALIDATION_DATA_DIR, 'rb') as f:
validation_data = pickle.load(f)
print '-->done!'
# Choose a random sample to monitor the training
num_random = random.choice(range(len(validation_data)))
validation_sample = validation_data[num_random]
cv2.imwrite('./' + DIR_TO_SAVE + '/validationRandomSaliencyGT.png', validation_sample.saliency.data)
cv2.imwrite('./' + DIR_TO_SAVE + '/validationRandomImage.png', cv2.cvtColor(validation_sample.image.data,
cv2.COLOR_RGB2BGR))
# Create network
if flag == 'salgan':
model = ModelSALGAN(INPUT_SIZE[0], INPUT_SIZE[1])
# Load a pre-trained model
# load_weights(net=model.net['output'], path="nss/gen_", epochtoload=15)
# load_weights(net=model.discriminator['prob'], path="test_dialted/disrim_", epochtoload=54)
salgan_batch_iterator(model, train_data, validation_sample.image.data)
elif flag == 'bce':
model = ModelBCE(INPUT_SIZE[0], INPUT_SIZE[1])
# Load a pre-trained model
# load_weights(net=model.net['output'], path='test/gen_', epochtoload=15)
bce_batch_iterator(model, train_data, validation_sample.image.data)
else:
print "Invalid input argument."