def get_points():
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((6*8,3), np.float32)
objp[:,:2] = np.mgrid[0:8, 0:6].T.reshape(-1 , 2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.
# Make a list of calibration images
images = glob.glob('calibration_wide/GO*.jpg')
# Step through the list and search for chessboard corners
for idx, fname in enumerate(images):
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (8,6), None)
# If found, add object points, image points
if ret == True:
objpoints.append(objp)
imgpoints.append(corners)
# Draw and display the corners
cv2.drawChessboardCorners(img, (8,6), corners, ret)
#write_name = 'corners_found'+str(idx)+'.jpg'
#cv2.imwrite(write_name, img)
cv2.imshow('img', img)
cv2.waitKey(500)
cv2.destroyAllWindows()
return objpoints, imgpoints
python类imwrite()的实例源码
data_preprocessing_autoencoder.py 文件源码
项目:AVSR-Deep-Speech
作者: pandeydivesh15
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def crop_and_store(frame, mouth_coordinates, name):
"""
Args:
1. frame: The frame which has to be cropped.
2. mouth_coordinates: The coordinates which help in deciding which region is to be cropped.
3. name: The path name to be used for storing the cropped image.
"""
# Find bounding rectangle for mouth coordinates
x, y, w, h = cv2.boundingRect(mouth_coordinates)
mouth_roi = frame[y:y + h, x:x + w]
h, w, channels = mouth_roi.shape
# If the cropped region is very small, ignore this case.
if h < 10 or w < 10:
return
resized = resize(mouth_roi, 32, 32)
cv2.imwrite(name, resized)
def test_image(addr):
target = ['angry','disgust','fear','happy','sad','surprise','neutral']
font = cv2.FONT_HERSHEY_SIMPLEX
im = cv2.imread(addr)
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1)
for (x, y, w, h) in faces:
cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 2,5)
face_crop = im[y:y+h,x:x+w]
face_crop = cv2.resize(face_crop,(48,48))
face_crop = cv2.cvtColor(face_crop, cv2.COLOR_BGR2GRAY)
face_crop = face_crop.astype('float32')/255
face_crop = np.asarray(face_crop)
face_crop = face_crop.reshape(1, 1,face_crop.shape[0],face_crop.shape[1])
result = target[np.argmax(model.predict(face_crop))]
cv2.putText(im,result,(x,y), font, 1, (200,0,0), 3, cv2.LINE_AA)
cv2.imshow('result', im)
cv2.imwrite('result.jpg',im)
cv2.waitKey(0)
def convert_wrapper(path, outpath, Debug=False):
for filename in sorted(os.listdir(path)):
if filename.endswith('.flo'):
filename = filename.replace('.flo','')
flow = read_flow(path, filename)
flow_img = convert_flow(flow, 2.0)
# NOTE: Change from BGR (OpenCV format) to RGB (Matlab format) to fit Matlab output
flow_img = cv2.cvtColor(flow_img, cv2.COLOR_BGR2RGB)
#print "Saving {}.png with shape: {}".format(filename, flow_img.shape)
cv2.imwrite(outpath + filename + '.png', flow_img)
if Debug:
ret = imchecker(outpath + filename)
# Sanity check and comparison if we have matlab version image
def CaptureImage():
imageName = 'DontCare.jpg' #Just a random string
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #For capture image in monochrome
rgbImage = frame #For capture the image in RGB color space
# Display the resulting frame
cv2.imshow('Webcam',rgbImage)
#Wait to press 'q' key for capturing
if cv2.waitKey(1) & 0xFF == ord('q'):
#Set the image name to the date it was captured
imageName = str(time.strftime("%Y_%m_%d_%H_%M")) + '.jpg'
#Save the image
cv2.imwrite(imageName, rgbImage)
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
#Returns the captured image's name
return imageName
def encode(img_path, wm_path, res_path, alpha):
img = cv2.imread(img_path)
img_f = np.fft.fft2(img)
height, width, channel = np.shape(img)
watermark = cv2.imread(wm_path)
wm_height, wm_width = watermark.shape[0], watermark.shape[1]
x, y = range(height / 2), range(width)
random.seed(height + width)
random.shuffle(x)
random.shuffle(y)
tmp = np.zeros(img.shape)
for i in range(height / 2):
for j in range(width):
if x[i] < wm_height and y[j] < wm_width:
tmp[i][j] = watermark[x[i]][y[j]]
tmp[height - 1 - i][width - 1 - j] = tmp[i][j]
res_f = img_f + alpha * tmp
res = np.fft.ifft2(res_f)
res = np.real(res)
cv2.imwrite(res_path, res, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
def find_lines(img):
edges = cv2.Canny(img,100,200)
threshold = 60
minLineLength = 10
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold, 0, minLineLength, 20);
if (lines is None or len(lines) == 0):
return
#print lines
for line in lines[0]:
#print line
cv2.line(img, (line[0],line[1]), (line[2],line[3]), (0,255,0), 2)
cv2.imwrite("line_edges.jpg", edges)
cv2.imwrite("lines.jpg", img)
def downscale(old_file_name):
img = cv2.imread(os.path.join(old_file_name))
new_file_name = (old_file_name
.replace('training', 'training_' + str(min_size))
.replace('validation', 'validation_' + str(min_size))
.replace('testing', 'testing_' + str(min_size))
)
height, width, _ = img.shape
if width > height:
new_width = int(1.0 * width / height * min_size)
new_height = min_size
else:
new_height = int(1.0 * height / width * min_size)
new_width = min_size
img_new = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_LINEAR)
cv2.imwrite(new_file_name, img_new)
def save_all_detection(im_array, detections, imdb_classes=None, thresh=0.7):
"""
save all detections in one image with result.png
:param im_array: [b=1 c h w] in rgb
:param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
:param imdb_classes: list of names in imdb
:param thresh: threshold for valid detections
:return:
"""
import random
im = image_processing.transform_inverse(im_array, config.PIXEL_MEANS)
im = im[:, :, ::-1].copy() # back to b,g,r
for j in range(1, len(imdb_classes)):
color = (255*random.random(), 255*random.random(), 255*random.random()) # generate a random color
dets = detections[j]
for i in range(dets.shape[0]):
bbox = dets[i, :4]
score = dets[i, -1]
if score > thresh:
cv2.rectangle(im, (int(round(bbox[0])), int(round(bbox[1]))),
(int(round(bbox[2])), int(round(bbox[3]))), color, 2)
cv2.putText(im, '%s'%imdb_classes[j], (bbox[0], bbox[1]),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, color, 2)
cv2.imwrite("result.jpg", im)
def write_song(piano_roll, filename):
""" Save the song on disk
Args:
piano_roll (np.array): a song object containing the tracks and melody
filename (str): the path were to save the song (don't add the file extension)
"""
note_played = piano_roll > 0.5
piano_roll_int = np.uint8(piano_roll*255)
b = piano_roll_int * (~note_played).astype(np.uint8) # Note silenced
g = np.zeros(piano_roll_int.shape, dtype=np.uint8) # Empty channel
r = piano_roll_int * note_played.astype(np.uint8) # Notes played
img = cv.merge((b, g, r))
# TODO: We could insert a first column indicating the piano keys (black/white key)
cv.imwrite(filename + '.png', img)
def on_mouse(event, x, y, flags, params):
# global img
t = time()
if event == cv2.EVENT_LBUTTONDOWN:
print 'Start Mouse Position: '+str(x)+', '+str(y)
sbox = [x, y]
boxes.append(sbox)
# print count
# print sbox
elif event == cv2.EVENT_LBUTTONUP:
print 'End Mouse Position: '+str(x)+', '+str(y)
ebox = [x, y]
boxes.append(ebox)
print boxes
crop = img[boxes[-2][1]:boxes[-1][1],boxes[-2][0]:boxes[-1][0]]
cv2.imshow('crop',crop)
k = cv2.waitKey(0)
if ord('r')== k:
cv2.imwrite('Crop'+str(t)+'.jpg',crop)
print "Written to file"
def find_bibs(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY);
binary = cv2.GaussianBlur(gray,(5,5),0)
ret,binary = cv2.threshold(binary, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU);
#binary = cv2.adaptiveThreshold(binary, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
#ret,binary = cv2.threshold(binary, 190, 255, cv2.THRESH_BINARY);
#lapl = cv2.Laplacian(image,cv2.CV_64F)
#gray = cv2.cvtColor(lapl, cv2.COLOR_BGR2GRAY);
#blurred = cv2.GaussianBlur(lapl,(5,5),0)
#ret,binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU);
#cv2.imwrite("lapl.jpg", lapl)
edges = cv2.Canny(image,175,200)
cv2.imwrite("edges.jpg", edges)
binary = edges
cv2.imwrite("binary.jpg", binary)
contours,hierarchy = find_contours(binary)
return get_rectangles(contours)
def test_featuredetector(self):
print self.photodir
#read in images
images = []
for i in np.arange(1,7):
images.append(cv2.imread(os.path.join(self.photodir,"Frosty5k","{}.jpg".format(i))))
#read in bib
bib = cv2.imread(os.path.join(self.photodir,"Frosty5k","bib.jpg"))
for i in np.arange(1,7):
image = images[i-1]
bib_kp, image_kp, matches = fd.findMatchesBetweenImages(bib, image)
output = fd.drawMatches(bib, bib_kp, image, image_kp, matches)
ftoutdir = os.path.join(self.photooutdir,"features")
print "Writing images to folder {}".format(ftoutdir)
if not os.path.exists(ftoutdir):
os.makedirs(ftoutdir)
cv2.imwrite(os.path.join(ftoutdir,"{}matches.jpg".format(i)), output)
def locate_img(image, template):
img = image.copy()
res = cv2.matchTemplate(img, template, method)
print res
print res.shape
cv2.imwrite('image/shape.png', res)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
print cv2.minMaxLoc(res)
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
h, w = template.shape
bottom_right = (top_left[0] + w, top_left[1]+h)
cv2.rectangle(img, top_left, bottom_right, 255, 2)
cv2.imwrite('image/tt.jpg', img)
def imshow_cv(label, im, block=False, text=None, wait=2):
vis = im.copy()
print_status(vis, text=text)
window_manager.imshow(label, vis)
ch = cv2.waitKey(0 if block else wait) & 0xFF
if ch == ord(' '):
cv2.waitKey(0)
if ch == ord('v'):
print('Entering debug mode, image callbacks active')
while True:
ch = cv2.waitKey(10) & 0xFF
if ch == ord('q'):
print('Exiting debug mode!')
break
if ch == ord('s'):
fn = 'img-%s.png' % time.strftime("%Y-%m-%d-%H-%M-%S")
print 'Saving %s' % fn
cv2.imwrite(fn, vis)
elif ch == 27 or ch == ord('q'):
sys.exit(1)
def save_images_grid(imgs, path, grid_w=4, grid_h=4, post_processing=postprocessing_tanh, transposed=False):
imgs = copy_to_cpu(imgs)
if post_processing is not None:
imgs = post_processing(imgs)
b, ch, w, h = imgs.shape
assert b == grid_w*grid_h
imgs = imgs.reshape((grid_w, grid_h, ch, w, h))
imgs = imgs.transpose(0, 1, 3, 4, 2)
if transposed:
imgs = imgs.reshape((grid_w, grid_h, w, h, ch)).transpose(1, 2, 0, 3, 4).reshape((grid_h*w, grid_w*h, ch))
else:
imgs = imgs.reshape((grid_w, grid_h, w, h, ch)).transpose(0, 2, 1, 3, 4).reshape((grid_w*w, grid_h*h, ch))
if ch==1:
imgs = imgs.reshape((grid_w*w, grid_h*h))
cv2.imwrite(path, imgs)
def s1_predict(config_file, model_dir, model_file, predict_file_list, out_dir):
"""
This function serves as a test/validation tool during the model development. It is not used as
a final product in part of the pipeline.
"""
with open(config_file) as config_buffer:
config = json.loads(config_buffer.read())
with tf.Graph().as_default() as graph:
converted_model = ConvertedModel(config, graph, 's1_keras', model_dir, model_file)
with tf.Session(graph=graph) as sess:
for img_file in predict_file_list:
image = cv2.imread(img_file)
boxes = converted_model.predict(sess, image)
image = draw_boxes(image, boxes)
_, filename = os.path.split(img_file)
cv2.imwrite(os.path.join(out_dir, filename), image)
def getTestImage(img,size):
size = int(size)
(x,y) = np.shape(img)
left,right,bottom,top = x,0,y,0
count = 0
for i in range(x):
for j in range(y):
if img[i][j] == 255:
left = min(left,i)
right = max(right,i)
top = max(top,j)
bottom = min(bottom,j)
count = count + 1
if count == 0:
return img
img = img[left:right,bottom:top]
cv2.imwrite('template.jpg',img)
return img
# Divides the grid into 9x9 = 81 cells and does OCR on each after processing it
def remove_specularity(img_files):
"""
Removes highlights/specularity in Images from multiple view points
:param img_files: File names of input images in horizontal order (important)
"""
# read images from file names
imgs = read_images(img_files)
# solve each pair of image.
# assumption: Input images are in order
for i in range(len(imgs) - 1):
logging.debug('processing images {} and {}'.format(i+1, i+2))
imgs[i], imgs[i+1] = _solve(imgs[i], imgs[i + 1])
for i, path in enumerate(img_files):
fname = os.path.basename(path)
res_file = os.path.join(RESULTS_DIR, fname)
logging.info('saving the results in {}'.format(res_file))
cv.imwrite(res_file, imgs[i])
def create_composite_image_coin_id(coin_id, crop_dir, data_dir):
images = []
images_gif = []
for id in range(0,56):
image_id = coin_id * 100 + id
crop = ci.get_rotated_crop(crop_dir, image_id, 56, 0)
images.append(crop)
filename = ci.get_filename_from(image_id,crop_dir)
images_gif.append(imageio.imread(filename))
composite_image = ci.get_composite_image(images, 8, 8)
cv2.imwrite(data_dir + str(coin_id) + '.png', composite_image)
imageio.mimsave(data_dir + str(coin_id) + '.gif', images_gif)
return
def cutout(self, img, cut_point, img_path='trim', padding=False, extra_cut=False):
'''??????????????image_path?????'''
px = self.padding_x if padding else 0
py = self.padding_y if padding else 0
cp_x = cut_point['x']
cp_y = cut_point['y']
for i in range(0, len(cp_y)):
if i % 2 == 0:
img_cut_1_4 = img[cp_y[i] - py:cp_y[i + 1] + py, cp_x[2] - px:cp_x[3] + px]
img_cut_5_8 = img[cp_y[i] - py:cp_y[i + 1] + py, cp_x[0] - px:cp_x[1] + px]
if extra_cut:
# ???????1???????????????????
img_cut_1_4 = hybrid_cut(img=img_cut_1_4, img_path='dum-{}'.format(i // 2 + 1))
img_cut_5_8 = hybrid_cut(img=img_cut_5_8, img_path='dum-{}'.format(i // 2 + 5))
cv2.imwrite('{}-{}.png'.format(img_path, str(i // 2 + 1)), img_cut_1_4)
cv2.imwrite('{}-{}.png'.format(img_path, str(i // 2 + 5)), img_cut_5_8)
def store_raw_images():
'''To download images from image-net
(Change the url for different needs of cascades)
'''
neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n07942152'
neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()
pic_num = 1
for i in neg_image_urls.split('\n'):
try:
print i
urllib.urlretrieve(i, "neg/" + str(pic_num) + '.jpg')
img = cv2.imread("neg/" + str(pic_num) +'.jpg',
cv2.IMREAD_GRAYSCALE)
resized_image = cv2.resize(img, (100, 100))
cv2.imwrite("neg/" + str(pic_num) + '.jpg', resized_image)
pic_num = pic_num + 1
except:
print "error"
def generate_avatar(dir, filename):
"""
????????????dir/avatar_filename
:return: ?????????bool?
"""
pil_image = numpy.array(Image.open(os.path.join(dir, filename)));
image = None;
try:
image = cv2.cvtColor(numpy.array(pil_image), cv2.COLOR_RGB2BGR);
except:
image = numpy.array(pil_image);
avatar = crop_avatar(image);
if avatar is None:
return False;
else:
cv2.imwrite(os.path.join(dir, "avatar_" + filename), avatar);
return True;
def __plot_canvas(self, show, save):
if len(self.result) == 0:
raise Exception('Please run blur_image() method first.')
else:
plt.close()
plt.axis('off')
fig, axes = plt.subplots(1, len(self.result), figsize=(10, 10))
if len(self.result) > 1:
for i in range(len(self.result)):
axes[i].imshow(self.result[i])
else:
plt.axis('off')
plt.imshow(self.result[0])
if show and save:
if self.path_to_save is None:
raise Exception('Please create Trajectory instance with path_to_save')
cv2.imwrite(os.path.join(self.path_to_save, self.image_path.split('/')[-1]), self.result[0] * 255)
plt.show()
elif save:
if self.path_to_save is None:
raise Exception('Please create Trajectory instance with path_to_save')
cv2.imwrite(os.path.join(self.path_to_save, self.image_path.split('/')[-1]), self.result[0] * 255)
elif show:
plt.show()
def dump_frames(vid_path):
import cv2
video = cv2.VideoCapture(vid_path)
vid_name = vid_path.split('/')[-1].split('.')[0]
out_full_path = os.path.join(out_path, vid_name)
fcount = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
try:
os.mkdir(out_full_path)
except OSError:
pass
file_list = []
for i in xrange(fcount):
ret, frame = video.read()
assert ret
cv2.imwrite('{}/{:06d}.jpg'.format(out_full_path, i), frame)
access_path = '{}/{:06d}.jpg'.format(vid_name, i)
file_list.append(access_path)
print '{} done'.format(vid_name)
sys.stdout.flush()
return file_list
def color_quant(input,K,output):
img = cv2.imread(input)
Z = img.reshape((-1,3))
# convert to np.float32
Z = np.float32(Z)
# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 15, 1.0)
ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
# Now convert back into uint8, and make original image
center = np.uint8(center)
res = center[label.flatten()]
res2 = res.reshape((img.shape))
cv2.imshow('res2',res2)
cv2.waitKey(0)
cv2.imwrite(output, res2)
cv2.destroyAllWindows()
view_pred_poly.py 文件源码
项目:kaggle-dstl-satellite-imagery-feature-detection
作者: u1234x1234
项目源码
文件源码
阅读 74
收藏 0
点赞 0
评论 0
def f(image_id):
# if os.path.exists('test_poly_{}_{}/{}.png'.format(version, epoch, image_id)):
# print(image_id)
# return
print('begin: {}'.format(image_id))
p = d[image_id]
p = [wkt.loads(x) for x in p]
y_sf, x_sf = get_scale_factor(image_id, size, size)
p = [affinity.scale(x, xfact=x_sf, yfact=y_sf, origin=(0, 0, 0)) for x in p]
rst = rasterize_polgygon(p, size, size)
color_rst = colorize_raster(rst)
im = get_rgb_image(image_id, size, size)
rr = np.hstack([color_rst, im])
cv2.imwrite('test_poly_{}_{}-cv/{}.png'.format(version, epoch, image_id), rr)
print('end: {}'.format(image_id))
def decode(ori_path, img_path, res_path, alpha):
ori = cv2.imread(ori_path)
img = cv2.imread(img_path)
ori_f = np.fft.fft2(ori)
img_f = np.fft.fft2(img)
height, width = ori.shape[0], ori.shape[1]
watermark = (ori_f - img_f) / alpha
watermark = np.real(watermark)
res = np.zeros(watermark.shape)
random.seed(height + width)
x = range(height / 2)
y = range(width)
random.shuffle(x)
random.shuffle(y)
for i in range(height / 2):
for j in range(width):
res[x[i]][y[j]] = watermark[i][j]
cv2.imwrite(res_path, res, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
def resize_image(img_path, mini_size=480, jpeg_quality=80):
"""
??image
:param img_path: image???
:param mini_size: ??????
:param jpeg_quality: jpeg?????
"""
org_img = cv2.imread(img_path)
img_w = org_img.shape[0]
img_h = org_img.shape[1]
if max(img_w, img_h) > mini_size:
if img_w > img_h:
img_w = mini_size * img_w // img_h
img_h = mini_size
else:
img_h = mini_size * img_h // img_w
img_w = mini_size
dist_size = (img_h, img_w)
r_image = cv2.resize(org_img, dist_size, interpolation=cv2.INTER_AREA)
params = [cv2.IMWRITE_JPEG_QUALITY, jpeg_quality]
img_name = img_path + '_New.jpg'
cv2.imwrite(img_name, r_image, params=[cv2.IMWRITE_JPEG_QUALITY, params])
build_of.py 文件源码
项目:Video-Classification-Action-Recognition
作者: qijiezhao
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def dump_frames(vid_path):
import cv2
video = cv2.VideoCapture(vid_path)
vid_name = vid_path.split('/')[-1].split('.')[0]
out_full_path = os.path.join(out_path, vid_name)
fcount = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
try:
os.mkdir(out_full_path)
except OSError:
pass
file_list = []
for i in xrange(fcount):
ret, frame = video.read()
assert ret
cv2.imwrite('{}/{:06d}.jpg'.format(out_full_path, i), frame)
access_path = '{}/{:06d}.jpg'.format(vid_name, i)
file_list.append(access_path)
print '{} done'.format(vid_name)
sys.stdout.flush()
return file_list