def test6(): # #读取图像到数组中 t = util.imageToTensor( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png") img = util.tensorToImage(t[0]) width = t[1] height = t[2] m = matrix.Matrix(height, width) m_point = matrix.Matrix(height, width) path = "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png" img = Image.open(path) # imgTensor.type(torch.DoubleTensor) scale = 0.6 angle = 30 translation = 0.1 new_img = tfs.functional.rotate(img, -angle) new_img = pic_strong(img).type(torch.DoubleTensor) img_torch = new_img # m.scale(1 / scale, 1 / scale) m.translation(-translation, -translation) # m_point.scale(scale, scale) # m_point.rotation(angle) m_point.translation_point(translation, translation) # theta = torch.tensor([[1, 0, 0.2], [0, 1, -0.4]], dtype=torch.double) theta = torch.from_numpy(m.to_theta()) grid = F.affine_grid(theta.unsqueeze(0), img_torch.unsqueeze(0).size(), False) output = F.grid_sample(img_torch.unsqueeze(0), grid) new_img_torch = output[0] # plt.imshow(new_img_torch.numpy().transpose(1, 2, 0)) img = util.tensorToImage(output) plt.imshow(img) points = util.textToPoint( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.pts") points = m_point.dot_point_68(points)[1] # 使用红色星状物标记绘制点 i = 1 for p in points: plt.plot(p[0], p[1], "r_") i = i + 1 plt.show() print("this is end")
def show(plt, X, L): plt.cla() img = util.tensorToImage(X) plt.imshow(img) points = util.tensorToPoint(L.cpu().detach()) for p in points: plt.plot(p[0] * Config.IMAGE_SIZE, p[1] * Config.IMAGE_SIZE, "r.") plt.savefig("/home/xws/Downloads/python/python/face/img/test0.png")
def show(model, data, path): imgTensor = data[0] X = imgTensor.view(1, 3, Config.IMAGE_SIZE, Config.IMAGE_SIZE) testout = model(X) points = util.tensorToPoint(testout.cpu().detach()) for p in points: plt.plot(p[0] * Config.IMAGE_SIZE, p[1] * Config.IMAGE_SIZE, "r+") img = util.tensorToImage(imgTensor) plt.imshow(img) plt.savefig(path[0].replace("300w_cropped/01_Indoor", "test")) # plt.show() plt.cla()
def test3(): # #读取图像到数组中 # im = array(Image.open('/home/xws/Downloads/boll.jpeg')) t = util.imageToTensor( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png") img = util.tensorToImage(t[0]) # img = plt.imread("/home/xws/Downloads/300w_cropped/01_Indoor/indoor_300.png") width = t[1] height = t[2] m = matrix.Matrix(height, width) m.rotation(30) path = "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png" img = Image.open(path) new_img = transform(img) new_img = tfs.functional.rotate(img, -30) plt.imshow(new_img) # text = util.readText("/home/xws/Downloads/300w_cropped/01_Indoor/indoor_300.pts") points = util.textToPoint( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.pts") points = m.dot_point_68(points)[1] # # 使用红色星状物标记绘制点 i = 1 for p in points: plt.plot(p[0], p[1], "r_") # plt.text(p[0], p[1], i) i = i + 1 # plt.text(points[8][0], points[8][1], 0) # plt.text(points[17][0], points[17][1], 0) # plt.text(points[26][0], points[26][1], 0) # plt.text(points[27][0], points[27][1], 0) # plt.text(points[33][0], points[33][1], 0) # plt.text(points[48][0], points[48][1], 0) # plt.text(points[54][0], points[54][1], 0) # plt.plot(points[8][0], points[8][1], "g|") plt.plot(points[17][0], points[17][1], "g|") plt.plot(points[26][0], points[26][1], "g|") # plt.plot(points[27][0], points[27][1], "g|") plt.plot(points[33][0], points[33][1], "g|") plt.plot(points[48][0], points[48][1], "g|") plt.plot(points[54][0], points[54][1], "g|") # #添加标题,显示绘制的图像 plt.show() print("this is end")
def test4(): # #读取图像到数组中 t = util.imageToTensor( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png") img = util.tensorToImage(t[0]) width = t[1] height = t[2] m = matrix.Matrix(height, width) path = "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png" img = Image.open(path) # new_img = transform(img) # new_img = tfs.functional.rotate(img, -30) new_img = tfs.functional.resize(img, [Config.IMAGE_SIZE, Config.IMAGE_SIZE], Image.BICUBIC) plt.imshow(img) plt.imshow(new_img) points = util.textToPoint( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.pts") # 将point扩充一下 newpoints = np.arange(68 * 3, dtype=float).reshape(68, 3) for i, p in enumerate(points): print("ww", i, p) newpoints[i][0] = p[0] newpoints[i][1] = p[1] newpoints[i][2] = 1 print(newpoints) m.rotation(30) # m.scale(1, 1) pp = m.dot(newpoints) for i, p in enumerate(pp): print("ww", i, p) points[i][0] = p[0] points[i][1] = p[1] # 使用红色星状物标记绘制点 i = 1 for p in points: plt.plot(p[0], p[1], "r_") i = i + 1 plt.show() print("this is end")
def test0(): plt.ion() # #读取图像到数组中 # im = array(Image.open('/home/xws/Downloads/boll.jpeg')) f = plt.figure() # #绘制图像 t = util.imageToTensor( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png") img = util.tensorToImage(t[0]) # img = plt.imread("/home/xws/Downloads/300w_cropped/01_Indoor/indoor_300.png") plt.imshow(img) width = t[1] height = t[2] # text = util.readText("/home/xws/Downloads/300w_cropped/01_Indoor/indoor_300.pts") points = util.textToPoint( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.pts") # print("points = ", points) points = util.pointToTensor(points) # print("pointToTensor = ", points) points = util.tensorToPoint(points) # print("tensorToPoint = ", points) # # 使用红色星状物标记绘制点 for p in points: plt.plot(p[0], p[1], "r+") # #绘制前两个点的线 # plt.plot(x[:2], y[:2]) # #添加标题,显示绘制的图像 plt.title('Plotting:"pic1.png"') plt.ioff() plt.savefig("/home/xws/Downloads/python/python/face/test.png") # plt.show() print("this is end")
# 加载小批次数据,即将MNIST数据集中的data分成每组batch_size的小块,shuffle指定是否随机读取 # train_loader = Data.DataLoader( # dataset=dataset.IBUGDataSet(Config.BATCH_SIZE), # batch_size=Config.BATCH_SIZE, # shuffle=True, # ) # load test testTensor = util.imageToTensor("/home/xws/Downloads/camera/25.jpeg") # testTensor = util.imageToTensor( # "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png" # ) imgTensor = testTensor[0] img = util.tensorToImage(testTensor[0]) plt.imshow(img) def test(): model.eval() # 模型转化为评估模式 if device == "cpu": X = imgTensor # 包装tensor用于自动求梯度 else: X = imgTensor.cuda() # 包装tensor用于自动求梯度 X = X.view(1, 3, Config.IMAGE_SIZE, Config.IMAGE_SIZE) testout = model(X) show(plt, X, testout)
def test7(): # #读取图像到数组中 t = util.imageToTensor( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png") img = util.tensorToImage(t[0]) width = t[1] height = t[2] m = matrix.Matrix(height, width) m_point = matrix.Matrix(height, width) path = "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.png" img = Image.open(path) # imgTensor.type(torch.DoubleTensor) scale = 0.6 angle = 30 translation = 0.1 new_img = tfs.functional.rotate(img, -angle) new_img = pic_strong(img).type(torch.DoubleTensor) img_torch = new_img m.scale(1 / scale, 1 / scale) m.translation(-translation, -translation) m_point.scale(scale, scale) # m_point.rotation(angle) m_point.translation_point(translation, translation) # theta = torch.tensor([[1, 0, 0.2], [0, 1, -0.4]], dtype=torch.double) theta = torch.from_numpy(m.to_theta()) grid = F.affine_grid(theta.unsqueeze(0), img_torch.unsqueeze(0).size(), False) output = F.grid_sample(img_torch.unsqueeze(0), grid) new_img_torch = output[0] plt.imshow(new_img_torch.numpy().transpose(1, 2, 0)) points = util.textToPoint( "/home/xws/Downloads/300w_cropped/01_Indoor/indoor_001.pts") point_tensor = util.pointToTensor(points) point_tensor = point_tensor.view(1, 68, 2) grid = F.affine_grid(theta.unsqueeze(0), point_tensor.unsqueeze(0).size(), False) output = F.grid_sample(point_tensor.unsqueeze(0), grid) points = util.tensorToPoint(output[0]) # 将point扩充一下 newpoints = np.arange(68 * 3, dtype=float).reshape(68, 3) for i, p in enumerate(points): newpoints[i][0] = p[0] newpoints[i][1] = p[1] newpoints[i][2] = 1 # m.scale(1, 1) pp = m_point.dot(newpoints) for i, p in enumerate(pp): points[i][0] = p[0] points[i][1] = p[1] # 使用红色星状物标记绘制点 i = 1 for p in points: plt.plot(p[0], p[1], "r_") i = i + 1 plt.show() print("this is end")
# img_ = np.rot90(img_, -1).copy() # if len(face) >= 1: # (x, y, w, h) = face[0] # cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # img = frame[:][y : y + h, x : x + w] # 如果分类器能捕捉到人脸,就对其进行剪裁送入网络,否则就将整张图片送入 # img = img.astype(np.uint8) image = Image.fromarray(img) # plt.imshow(image) # plt.savefig("/home/xws/Downloads/camera/" + str(camera_index)) # plt.show() # plt.cla() camera_index = camera_index + 1 tensor = transforms(image) img = util.tensorToImage(tensor) plt.imshow(img) plt.savefig("/home/xws/Downloads/camera/" + str(camera_index) + ".jpeg") plt.show() plt.cla() tensor1 = tensor.reshape(1, 3, Config.IMAGE_SIZE, Config.IMAGE_SIZE) pre = model(tensor1) # util.show(plt,tensor,pre) points = util.tensorToPoint(pre.detach()) for p in points: p[0] = p[0] * 480 p[1] = p[1] * 640 cv2.circle(img_, (int(p[0]), int(p[1])), point_size, point_color, thickness) # cv2.rectangle(img_, (int(-10), int(0)), (int(200), int(300)), (0, 255, 0))