python+ opencv实现摄像头实时人脸识别并实现汉字标框

opencv的puttxt()函数不能汉字输出,这也是困惑好多人都问题,经过几天的查资料,改代码终于成功实现opencv汉字输出。

第一种方法是 是通过写一段代码,能够转码,封装一下再调用,从而实现汉字输出。

第二种方法是 使用PIL进行转换一下

以下这个是ft2.py  实现转码的代码

# -*- coding: utf-8 -*-
# http://blog.csdn.net/zizi7/article/details/70145150
                                                      
import numpy as np
import freetype
import copy
import pdb

class put_chinese_text(object):
    def __init__(self, ttf):
        self._face = freetype.Face(ttf)

    def draw_text(self, image, pos, text, text_size, text_color):
        '''
        draw chinese(or not) text with ttf
        :param image:     image(numpy.ndarray) to draw text
        :param pos:       where to draw text
        :param text:      the context, for chinese should be unicode type
        :param text_size: text size
        :param text_color:text color
        :return:          image
        '''
        self._face.set_char_size(text_size * 64)
        metrics = self._face.size
        ascender = metrics.ascender/64.0

        #descender = metrics.descender/64.0
        #height = metrics.height/64.0
        #linegap = height - ascender + descender
        ypos = int(ascender)

        if not isinstance(text, str):
            #对于Python 2中的unicode和Python 3中的str,对于Python 2中的str/bytes和Python 3中的bytes的二进制文件
            text = text.decode('utf-8')
        img = self.draw_string(image, pos[0], pos[1]+ypos, text, text_color)
        return img

    def draw_string(self, img, x_pos, y_pos, text, color):
        '''
        draw string
        :param x_pos: text x-postion on img
        :param y_pos: text y-postion on img
        :param text:  text (unicode)
        :param color: text color
        :return:      image
        '''
        prev_char = 0
        pen = freetype.Vector()
        pen.x = x_pos << 6   # div 64
        pen.y = y_pos << 6

        hscale = 1.0
        matrix = freetype.Matrix(int((hscale)*0x10000), int(0.2*0x10000),\
                                 int(0.0*0x10000), int(1.1*0x10000))
        cur_pen = freetype.Vector()
        pen_translate = freetype.Vector()

        image = copy.deepcopy(img)
        for cur_char in text:
            self._face.set_transform(matrix, pen_translate)

            self._face.load_char(cur_char)
            kerning = self._face.get_kerning(prev_char, cur_char)
            pen.x += kerning.x
            slot = self._face.glyph
            bitmap = slot.bitmap

            cur_pen.x = pen.x
            cur_pen.y = pen.y - slot.bitmap_top * 64
            self.draw_ft_bitmap(image, bitmap, cur_pen, color)

            pen.x += slot.advance.x
            prev_char = cur_char

        return image

    def draw_ft_bitmap(self, img, bitmap, pen, color):
        '''
        draw each char
        :param bitmap: bitmap
        :param pen:    pen
        :param color:  pen color e.g.(0,0,255) - red
        :return:       image
        '''
        x_pos = pen.x >> 6
        y_pos = pen.y >> 6
        cols = bitmap.width
        rows = bitmap.rows

        glyph_pixels = bitmap.buffer

        for row in range(rows):
            for col in range(cols):
                if glyph_pixels[row*cols + col] != 0:
                    img[y_pos + row][x_pos + col][0] = color[0]
                    img[y_pos + row][x_pos + col][1] = color[1]
                    img[y_pos + row][x_pos + col][2] = color[2]


if __name__ == '__main__':
    # just for test
    import cv2

    line = '你好'
    img = np.zeros([300,300,3])

    color_ = (0,255,0) # Green
    pos = (3, 3)
    text_size = 24

    #ft = put_chinese_text('wqy-zenhei.ttc')
    ft = put_chinese_text('msyh.ttf')
    image = ft.draw_text(img, pos, line, text_size, color_)

    cv2.imshow('diplay', image)
    cv2.waitKey(0)

里面包含了几个函数,其中需要一个字体 msyh.ttf在同一文件夹下

(有时候这种方法实现不了,也可以使用PIL库转换的方法,不过那种方法的缺点在于需要加载库,不过简单易懂)

以下是实现人脸识别的代码

# -*- coding: utf-8 -*-
# 摄像头头像识别
import face_recognition
import cv2
import ft2

source = "rtsp://admin:5417010101xx@192.168.1.61/Streaming/Channels/1"
cam = cv2.VideoCapture(source)

# 本地图像
zwh_image = face_recognition.load_image_file("zwh.jpg")
zwh_face_encoding = face_recognition.face_encodings(zwh_image)[0]

# 本地图像二
chenduling_image = face_recognition.load_image_file("chenduling.jpg")
chenduling_face_encoding = face_recognition.face_encodings(chenduling_image)[0]

# 本地图片三
liujunbo_image = face_recognition.load_image_file("liujunbo.jpg")
liujunbo_face_encoding = face_recognition.face_encodings(liujunbo_image)[0]

# Create arrays of known face encodings and their names
# 脸部特征数据的集合
known_face_encodings = [
    zwh_face_encoding,
    chenduling_face_encoding,
    liujunbo_face_encoding
]

# 人物名称的集合
known_face_names = [
    "张文豪",
    "陈都灵",
    "刘军波"
]

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while(cam.isOpened()):
    # 读取摄像头画面
    ret, frame = cam.read()
    if not ret:
        #等同于 if ret is not none
        break

    # 改变摄像头图像的大小,图像小,所做的计算就少
    small_frame = cv2.resize(frame, (0, 0), fx=0.33, fy=0.33)

    # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。
    rgb_small_frame = small_frame[:, :, ::-1]

    # Only process every other frame of video to save time
    if process_this_frame:
        # 根据encoding来判断是不是同一个人,是就输出true,不是为flase
        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:
            # 默认为unknown
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding,tolerance=0.48)
            #阈值太低容易造成无法成功识别人脸,太高容易造成人脸识别混淆 默认阈值tolerance为0.6
            #print(matches)
            name = "Unknown"

            # if match[0]:
            #     name = "michong"
            # If a match was found in known_face_encodings, just use the first one.
            if True in matches:
                first_match_index = matches.index(True)
                name = known_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):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        #由于我们检测到的帧被缩放到1/4大小,所以要缩小面位置
        top *= 3
        right *= 3
        bottom *= 3
        left *= 3

        # 矩形框
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 3)
        ft = ft2.put_chinese_text('msyh.ttf')
        #引入ft2中的字体
        #加上标签
        #cv2.rectangle(frame, (left, bottom - 20), (right, bottom), (0, 0, 255), cv2.FILLED)
       # cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.8, (255, 255, 255), 1)这是不输入汉字时可以用的代码

        frame = ft.draw_text(frame, (left+10 , bottom ), name, 20, (255, 255, 255))

         #cv2.imshow("frame",image)会出来两个框一个monitor 一个frame后者显示image但只有frame为true时才会显示
        #def draw_text(self, image, pos, text, text_size, text_color)

    cv2.imshow('monitor', frame)
    if cv2.waitKey(1) & 0xFF == 27:
        break

cam.release()
cv2.destroyAllWindows()

使用PIL转换  由于cv2和PIL中颜色的hex码储存顺序不同,需要进行转换   下面这段代码可以替换上面的汉字标框的方法。

 cv2img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # cv2和PIL中颜色的hex码的储存顺序不同
pilimg = Image.fromarray(cv2img)
 draw = ImageDraw.Draw(pilimg) # 图片上打印
font = ImageFont.truetype("msyh.ttf", 27, encoding="utf-8") # 参数1:字体文件路径,参数2:字体大小
draw.text((left+10 , bottom ), name, (220, 20, 60), font=font) # 参数1:打印坐标,参数2:文本,参数3:字体颜色,参数4:字体

            # PIL图片转cv2 图片
frame = cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)

 

最基本的要装上python和opencv,距离远不能看出人脸的解决方法 :可以通过调整获取每帧图像的大小可以进行改造,代价就是会变卡,延迟高,这就需要通过调整摄像头的参数进行修改分辨率视频帧率 码率上限 视频编码来进行降低分辨率。

人脸混淆较近看不出人脸的解决方法:修改阈值 阈值太低容易造成无法成功识别人脸,太高容易造成人脸识别混淆 默认阈值tolerance为0.6 亚洲人一般要用低点

 

https://github.com/niehen6174/face-recognition-and-put-in-chinese/tree/master/code

上面链接有完整代码  字体和一个照片文件  

其他人脸识别模块介绍   https://blog.csdn.net/Nirvana_6174/article/details/89599441

如有问题,或有什么建议可加群:894243022或发邮箱1639206518@qq.com 

使用本文章或代码还请声明。

  • 17
    点赞
  • 192
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
好的,要使用Java OpenCV调用摄像头并进行文字识别,您需要遵循以下步骤: 1. 安装Tesseract OCR引擎 Tesseract OCR引擎是一个开源的OCR引擎,可以识别多种语言的文本。您可以从Tesseract OCR官方网站下载并安装Tesseract OCR引擎。 2. 导入Tesseract OCR库 在Java项目中,您需要导入Tesseract OCR库。您可以使用Maven和Gradle等构建工具来导入Tesseract OCR库。 3. 调用摄像头 您可以使用Java OpenCV中的VideoCapture类调用摄像头。以下是一个简单的示例代码: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.videoio.VideoCapture; public class CameraCapture { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); if(!camera.isOpened()){ System.out.println("Error"); } else { Mat frame = new Mat(); while(true){ if (camera.read(frame)){ System.out.println("Frame Obtained"); System.out.println("Captured Frame Width " + frame.width() + " Height " + frame.height()); Core.flip(frame, frame, 1); break; } } } camera.release(); } } ``` 此代码段将获取从摄像头获取的帧并将其翻转。 4. 文字识别 要进行文字识别,您可以使用Java OpenCV中的Imgcodecs类将帧转换为图像,然后使用Tesseract OCR库进行识别。以下是一个简单的示例代码: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.videoio.VideoCapture; import net.sourceforge.tess4j.*; public class TextRecognition { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); Mat frame = new Mat(); while (true){ if (camera.read(frame)){ Mat grayFrame = new Mat(); Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY); File imageFile = new File("image.png"); Imgcodecs.imwrite(imageFile.getAbsolutePath(), grayFrame); ITesseract tess = new Tesseract(); tess.setDatapath("tessdata"); tess.setLanguage("eng"); String result = tess.doOCR(imageFile); System.out.println(result); HighGui.imshow("Text Recognition", frame); HighGui.waitKey(1); } } } } ``` 此代码段将在从摄像头获取的帧中识别文本。它首先将帧转换为灰度图像,然后将其保存到磁盘上的图像文件中。然后,它使用Tesseract OCR库对图像文件中的文本进行识别,并将结果打印到控制台上。您需要将Tesseract OCR库的数据路径设置为“tessdata”文件夹,其中包含识别语言的数据文件。 希望这可以帮助您开始使用Java OpenCV调用摄像头并进行文字识别

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值