文本轮廓检测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | from openvino.inference_engine import IECore import numpy as np import time import cv2 as cv def text_detection_demo(): ie = IECore() for device in ie.available_devices: print (device) model_xml = "/home/bhc/BHC/model/intel/text-detection-0004/FP16/text-detection-0004.xml" model_bin = "/home/bhc/BHC/model/intel/text-detection-0004/FP16/text-detection-0004.bin" net = ie.read_network(model = model_xml, weights = model_bin) input_blob = next ( iter (net.input_info)) text_it = iter (net.outputs) out_blob1 = next (text_it) # model/link_logits_/add #输出(1, 16, 192, 320) out_blob2 = next (text_it) # model/segm_logits/add #(1, 2, 192, 320)2:text\no-text print (out_blob1, out_blob2) n, c, h, w = net.input_info[input_blob].input_data.shape print (n, c, h, w) src = cv.imread( "002.png" ) image = cv.resize(src, (w, h)) image = image.transpose( 2 , 0 , 1 ) exec_net = ie.load_network(network = net, device_name = "CPU" ) res = exec_net.infer(inputs = {input_blob:[image]}) res = res[out_blob2] res = np.squeeze(res, 0 ) res = res.transpose( 1 , 2 , 0 ) res = np.argmax(res, 2 ) hh, ww = res.shape mask = np.zeros((hh, ww), dtype = np.uint8) #灰度图像 mask[np.where(res > 0 )] = 255 #text则白色 mask = cv.resize(mask, (src.shape[ 1 ], src.shape[ 0 ])) ret, binary = cv.threshold(mask, 127 , 255 , cv.THRESH_BINARY) #阈值二值化,大于127则255 cv.imshow( "mask" , binary) # result = cv.addWeighted(src, 0.5, mask, 0.5, 0) contours, hiearchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) #轮廓检测 for cnt in range ( len (contours)): x, y, w, h = cv.boundingRect(contours[cnt]) #轮廓矩形 cv.rectangle(src, (x, y), (x + w, y + h), ( 244 , 255 , 0 ), 2 , 8 , 0 ) cv.imshow( "Text Detection" , src) cv.waitKey( 0 ) if __name__ = = "__main__" : text_detection_demo() |
天道酬勤 循序渐进 技压群雄
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端