RGB问题

关于numpy的矩阵的翻转(对于上述的方法2的说明)

 

上述方法2正是利用了numpy对数组的翻转。举例说明。

我仿照图片的像素格式,建立一个2行3列的,每个像素有包含RGB3个元素。

分别进行图中所示的4种运算。

执行a[:-1],移除了后面的一行。对于一维数组,后面的一行其实就是最后一个元素,所以这个运算就是移除最后一个元素。

执行a[::-1],上下两行交换了。同样的看成一维数组的话,一行就是一个元素,这个运算其实就是对一个一维数组内的元素前后对调。多维数组可以理解成对对第一个方括号内的每一个元素前后对调。

执行a[:,::-1],每一行中的元素前后交换了。简单理解就是对第二层反括号内的元素前后对调。

执行a[:,:,::-1],这样就好理解了,肯定是对第三层方括号内的元素对调。这也就解释了,对于一个24位深度的图像执行这个操作的话,是对每个像素的RGB进行对调。

对于图像而言,a[::-1],a[:,::-1],a[:,:,::-1]上述的三种方法分别是X轴的镜像,Y轴的镜像,BGR转换为RGB的操作。

示例:

import cv2 import face_recognition import numpy as np from PIL import Image, ImageDraw,ImageFont video_capture = cv2.VideoCapture(r'C:/Users/ALIENWARE/123.mp4')#如果输入是(0)为摄像头输入 #现输入为MP4进行识别检测人脸 first_image = face_recognition.load_image_file("1.jpg") first_face_encoding = face_recognition.face_encodings(first_image)[0] Second_image = face_recognition.load_image_file("2.jpg") Second_face_encoding = face_recognition.face_encodings(Second_image)[0] third_image = face_recognition.load_image_file("3.jpg") third_face_encoding = face_recognition.face_encodings(third_image)[0] inside_face_encodings = [first_face_encoding,Second_face_encoding,third_face_encoding] inside_face_names = ['A','B','C'] face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: ret, frame = video_capture.read() small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) rgb_small_frame = small_frame[:, :, ::-1] if process_this_frame: face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] for face_encoding in face_encodings: matches = face_recognition.compare_faces(inside_face_encodings, face_encoding) name = '未录入人脸' if True in matches: first_match_index = matches.index(True) name = inside_face_names[first_match_index] face_names.append(name) process_this_frame = not process_this_frame for (top, right, bottom, left), name in zip(face_locations, face_names): top *= 4 right *= 4 bottom *= 4 left *= 4 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) img_pil = Image.fromarray(frame) draw = ImageDraw.Draw(img_pil) fontStyle = ImageFont.truetype("C:/Windows/Fonts/simsun.ttc", 32, encoding="utf-8") draw.text((left + 6, bottom - 6), name, (0, 200, 0), font=fontStyle) frame = np.asarray(np.array(img_pil)) cv2.imshow('face_out', frame) if cv2.waitKey(1) & 0xFF == ord('q'): #退出需要按下Q键否则内核会崩溃 break video_capture.release() cv2.destroyAllWindows()
06-07
以下是修改后的代码,主要是添加了注释和一些规范化的修改: ```python import cv2 import face_recognition import numpy as np from PIL import Image, ImageDraw, ImageFont # 加载要识别的人脸图片 first_image = face_recognition.load_image_file("1.jpg") first_face_encoding = face_recognition.face_encodings(first_image)[0] Second_image = face_recognition.load_image_file("2.jpg") Second_face_encoding = face_recognition.face_encodings(Second_image)[0] third_image = face_recognition.load_image_file("3.jpg") third_face_encoding = face_recognition.face_encodings(third_image)[0] # 存储要检测的人脸编码及对应的人名 inside_face_encodings = [first_face_encoding, Second_face_encoding, third_face_encoding] inside_face_names = ['A', 'B', 'C'] # 打开视频流 video_capture = cv2.VideoCapture(r'C:/Users/ALIENWARE/123.mp4') # 循环读取视频帧 while True: # 读取一帧 ret, frame = video_capture.read() # 降低帧率,加快处理速度 if not ret: break # 缩小图片尺寸,加快处理速度 small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) rgb_small_frame = small_frame[:, :, ::-1] # 处理每一帧图片 face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] # 遍历每个检测到的人脸 for face_encoding in face_encodings: # 比对人脸编码 matches = face_recognition.compare_faces(inside_face_encodings, face_encoding) name = '未录入人脸' # 找到匹配的人脸 if True in matches: first_match_index = matches.index(True) name = inside_face_names[first_match_index] face_names.append(name) # 在画面上标记人脸及名字 for (top, right, bottom, left), name in zip(face_locations, face_names): # 将坐标放大4倍,因为之前缩小了4倍 top *= 4 right *= 4 bottom *= 4 left *= 4 # 画出人脸框 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # 添加名字 img_pil = Image.fromarray(frame) draw = ImageDraw.Draw(img_pil) fontStyle = ImageFont.truetype("C:/Windows/Fonts/simsun.ttc", 32, encoding="utf-8") draw.text((left + 6, bottom - 6), name, (0, 200, 0), font=fontStyle) frame = np.asarray(np.array(img_pil)) # 显示画面 cv2.imshow('face_out', frame) # 按下q键退出程序 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放视频流 video_capture.release() # 关闭所有窗口 cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值