空港防疫算法交互
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
8.3KB

  1. import time
  2. from io import BytesIO
  3. import cv2
  4. import requests
  5. from PIL import Image, ImageDraw, ImageFont
  6. import numpy as np
  7. '''
  8. 文字水印
  9. '''
  10. class TextWaterMark():
  11. def __init__(self):
  12. self.color_dict = {
  13. # R G B
  14. # 网址查看:https://tool.oschina.net/commons?type=3
  15. # 网址查看:http://tools.jb51.net/static/colorpicker/
  16. 'white': (255, 255, 255, 255),
  17. 'black': (0, 0, 0, 255),
  18. 'gray': (205, 201, 201, 255),
  19. 'red': (255, 0, 0, 255),
  20. 'yellow': (255, 215, 0, 255),
  21. 'blue': (0, 0, 170, 255),
  22. 'purple': (205, 105, 201, 255),
  23. 'green': (0, 205, 0, 255)
  24. }
  25. self.position_list = [1, 2, 3, 4]
  26. """
  27. 普通照片水印
  28. params:
  29. image:图片
  30. text:水印文字
  31. position:水印位置
  32. 1:左上
  33. 2:右上
  34. 3:右下
  35. 4:左下
  36. fontface: 字体
  37. fontsize:字体大小
  38. fontcolor:字体颜色
  39. [white, black, gray, red, yellow, blue, purple, green]
  40. """
  41. def common_water(self, image, text, position=1, fontface='../font/simsun.ttc', fontsize=20, fontcolor='black'):
  42. flag = False
  43. if isinstance(image, np.ndarray): # 判断是否OpenCV图片类型
  44. flag = True
  45. image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA))
  46. if position not in self.position_list:
  47. position = 1
  48. w, h = image.size[:2]
  49. keys = self.color_dict.keys()
  50. if fontcolor not in keys:
  51. fontcolor = 'black'
  52. color = self.color_dict[fontcolor]
  53. fnt = ImageFont.truetype(fontface, fontsize)
  54. im = image.convert('RGBA')
  55. mask = Image.new('RGBA', im.size, (0, 0, 0, 0))
  56. d = ImageDraw.Draw(mask)
  57. size_w, size_h = d.textsize(text, font=fnt)
  58. if position == 1:
  59. weizhi = (w * 0.1, h * 0.1)
  60. elif position == 2:
  61. weizhi = (w * 0.9 - size_w, h * 0.1)
  62. elif position == 3:
  63. weizhi = (w * 0.9 - size_w, h * 0.9 - size_h)
  64. else:
  65. weizhi = (w * 0.1, h * 0.9 - size_h)
  66. # position 为左上角位置
  67. d.text(weizhi, text, font=fnt, fill=color)
  68. out = Image.alpha_composite(im, mask)
  69. if flag:
  70. out = cv2.cvtColor(np.asarray(out), cv2.COLOR_BGRA2RGBA)
  71. return out
  72. """
  73. 半透明水印,布满整张图,并且自动旋转45°
  74. params:
  75. image:图片
  76. text:文字
  77. fontsize:文字大小
  78. """
  79. def fill_water(self, image, text, fontsize):
  80. flag = False
  81. if isinstance(image, np.ndarray): # 判断是否OpenCV图片类型
  82. flag = True
  83. image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA))
  84. font = ImageFont.truetype('../font/simsun.ttc', fontsize)
  85. # 添加背景
  86. new_img = Image.new('RGBA', (image.size[0] * 3, image.size[1] * 3), (255, 255, 255, 255))
  87. new_img.paste(image, image.size)
  88. # 添加水印
  89. font_len = len(text)
  90. rgba_image = new_img.convert('RGBA')
  91. text_overlay = Image.new('RGBA', rgba_image.size, (0, 0, 0, 0))
  92. image_draw = ImageDraw.Draw(text_overlay)
  93. for i in range(0, rgba_image.size[0], font_len * 40 + 100):
  94. for j in range(0, rgba_image.size[1], 200):
  95. # print(f'i:{i}, j:{j}, text:{text}, font:{font}')
  96. image_draw.text((i, j), text, font=font, fill=(0, 0, 0, 50))
  97. text_overlay = text_overlay.rotate(-45)
  98. image_with_text = Image.alpha_composite(rgba_image, text_overlay)
  99. image_with_text = image_with_text.crop((image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))
  100. if flag:
  101. image_with_text = cv2.cvtColor(np.asarray(image_with_text), cv2.COLOR_BGRA2RGBA)
  102. return image_with_text
  103. class PictureWaterMark:
  104. def __init__(self):
  105. self.logo = cv2.imread("./image/logo.png", -1)
  106. def common_water(self, image, logo):
  107. width, height = image.shape[1], image.shape[0]
  108. mark_width, mark_height = logo.shape[1], logo.shape[0]
  109. rate = int(width * 0.2) / mark_width
  110. logo_new = cv2.resize(logo, None, fx=rate, fy=rate, interpolation=cv2.INTER_NEAREST)
  111. position = (int(width * 0.95 - logo_new.shape[1]), int(height * 0.95 - logo_new.shape[0]))
  112. b = Image.new('RGBA', (width, height), (0, 0, 0, 0)) # 创建新图像:透明'
  113. a = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
  114. watermark = Image.fromarray(cv2.cvtColor(logo_new, cv2.COLOR_BGRA2RGBA))
  115. # 图片旋转
  116. # watermark = watermark.rotate(45)
  117. b.paste(a, (0, 0))
  118. b.paste(watermark, position, mask=watermark)
  119. return cv2.cvtColor(np.asarray(b), cv2.COLOR_BGR2RGB)
  120. def common_water_1(self, image, logo, alpha=1):
  121. h, w = image.shape[0], image.shape[1]
  122. if w >= h:
  123. rate = int(w * 0.1) / logo.shape[1]
  124. else:
  125. rate = int(h * 0.1) / logo.shape[0]
  126. mask = cv2.resize(logo, None, fx=rate, fy=rate, interpolation=cv2.INTER_NEAREST)
  127. mask_h, mask_w = mask.shape[0], mask.shape[1]
  128. mask_channels = cv2.split(mask)
  129. dst_channels = cv2.split(image)
  130. # b, g, r, a = cv2.split(mask)
  131. # 计算mask在图片的坐标
  132. ul_points = (int(h * 0.95) - mask_h, int(w - h * 0.05 - mask_w))
  133. dr_points = (int(h * 0.95), int(w - h * 0.05))
  134. for i in range(3):
  135. dst_channels[i][ul_points[0]: dr_points[0], ul_points[1]: dr_points[1]] = dst_channels[i][
  136. ul_points[0]: dr_points[0],
  137. ul_points[1]: dr_points[1]] * (
  138. 255.0 - mask_channels[3] * alpha) / 255
  139. dst_channels[i][ul_points[0]: dr_points[0], ul_points[1]: dr_points[1]] += np.array(
  140. mask_channels[i] * (mask_channels[3] * alpha / 255), dtype=np.uint8)
  141. dst_img = cv2.merge(dst_channels)
  142. return dst_img
  143. #差值感知算法
  144. def dHash(image):
  145. #缩放9*8
  146. image=cv2.resize(image,(9,8),interpolation=cv2.INTER_CUBIC)
  147. #转换灰度图
  148. image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  149. # print(image.shape)
  150. hash=[]
  151. #每行前一个像素大于后一个像素为1,相反为0,生成哈希
  152. for i in range(8):
  153. for j in range(8):
  154. if image[i,j]>image[i,j+1]:
  155. hash.append(1)
  156. else:
  157. hash.append(0)
  158. return hash
  159. #计算汉明距离
  160. def Hamming_distance(hash1,hash2):
  161. num = 0
  162. for index in range(len(hash1)):
  163. if hash1[index] != hash2[index]:
  164. num += 1
  165. return num
  166. def url2Array(url):
  167. response = requests.get(url)
  168. image = Image.open(BytesIO(response.content))
  169. image1= np.array(image)
  170. img_bgr = cv2.cvtColor(image1, cv2.COLOR_RGB2BGR)
  171. return img_bgr
  172. if __name__ == '__main__':
  173. # img = cv2.imread("../test/a.jpg", -1)
  174. # fontcolor = 'yellow'
  175. #
  176. # water = TextWaterMark()
  177. # text = "hello world"
  178. #
  179. # # fill_img = water.common_water(img, text, position=4, fontface='../font/庞门正道标题体2.0增强版.ttf', fontsize=20, fontcolor='black')
  180. # fill_img = water.fill_water(img, text, 20)
  181. # # 一定要保存为png格式
  182. # cv2.imshow('result', fill_img)
  183. # cv2.waitKey(111111110)
  184. # print('finish')
  185. pic = PictureWaterMark()
  186. image = cv2.imread("a.jpg")
  187. logo = cv2.imread("../image/logo.png", -1)
  188. # print(image, logo)
  189. start = time.time()
  190. frame = pic.common_water(image, logo)
  191. print(time.time() - start)
  192. start1 = time.time()
  193. frame1 = pic.common_water_1(image, logo)
  194. # cv2.imwrite("watermarked.jpg", frame1)
  195. print(time.time() - start1)
  196. # cap = cv2.VideoCapture("../data/111111.mp4")
  197. # logo = cv2.imread("../image/logo.png", -1)
  198. # while True:
  199. # is_opened, frame = cap.read()
  200. # frame = pic.common_water(frame, logo)
  201. # cv2.imshow('frame', frame)
  202. # cv2.waitKey(1) # 等待输入任何按键
  203. # # 关闭
  204. # cap.release()