视频识别成文字php,视频字幕和文字提取器-本地识别模型-人工智能文字识别源码交流...
[Python] 純文本查看 復制代碼import paddlehub as hub
import cv2,copy,os
import time
class Ocr():
def __init__(self,mobile):
if mobile=='否':
# 加載移動端預訓練模型
self.ocr = hub.Module(name="chinese_ocr_db_crnn_mobile")
else:
# 服務端可以加載大模型,效果更好
self.ocr = hub.Module(name="chinese_ocr_db_crnn_server")
def get_text(self,image,visualization,gpu):
if gpu ==True:
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
text=''
results = self.ocr.recognize_text(
images=[image], # 圖片數據,ndarray.shape 為 [H, W, C],BGR格式;
use_gpu=gpu, # 是否使用 GPU;若使用GPU,請先設置CUDA_VISIBLE_DEVICES環境變量
output_dir='ocr_result', # 圖片的保存路徑,默認設為 ocr_result;
visualization=visualization, # 是否將識別結果保存為圖片文件;
box_thresh=0.5, # 檢測文本框置信度的閾值;
text_thresh=0.5) # 識別中文文本置信度的閾值;
data=results[0]['data']
save_path = results[0]['save_path']
for infomation in data:
text=text+' '+infomation['text']
return text,save_path
if __name__ == '__main__':
time_start=time.time()
with open('配置.txt', 'r',encoding='utf-8')as f:
setting = f.read()
setting = setting.split('\n')
voidPath=setting[0].split('"')[1]
timeS = int(setting[1].split('"')[1])
testPath = setting[2].split('"')[1]
show=setting[3].split('"')[1]
mobile=setting[4].split('"')[1]
gpu=setting[5].split('"')[1]
print("視頻地址:", voidPath)
print('判斷幀間隔:', timeS)
print('識別結果保存目錄:', testPath)
print('是否可視化:', show)
print('精準識別:', mobile)
print('GPU:', gpu)
if gpu=='是':
gpu=True
else:
gpu=False
c = 0
cap = cv2.VideoCapture(voidPath)
last_text = ''
now_text = ''
text = ''
orc=Ocr(mobile)
#返回所有幀數
fps=int(cap.get(cv2.CAP_PROP_FPS))
#frames_num = cap.get(7)
try:
while (cap.isOpened()):
t1=time.time()
now_text = ''
c = c + 1
ret, frame = cap.read()
if c % timeS == 0: # c除盡timeS時取幀保存,圖片,即隔timeS保存一次圖片
#運算時間
m, s = divmod(c//fps, 60)
h, m = divmod(m, 60)
if show=='是':
now_text, save_path = orc.get_text(frame, True,gpu)
try:
image = cv2.imread(save_path)
x, y = image.shape[0:2]
image=cv2.resize(image, (int(y / 2), int(x / 2)))
cv2.imshow('Video Subtitle Extraction', image)
k = cv2.waitKey(60)
# q鍵退出
if (k & 0xff == ord('q')):
break
except:
pass
else:
now_text, save_path = orc.get_text(frame,False )
if now_text!=last_text:#判斷是否重復
text=now_text +f'[{h}:{m}:{s}]'+ '\n'
last_text=copy.copy(now_text)
print(text,f'用時:{time.time()-t1}')
with open(testPath, 'a+')as f:
f.write(text)
except Exception as err:
print(err)
cap.release()
cv2.destroyAllWindows()
time_stop = time.time()
input(f'全部完成用時:{time_stop-time_start}')
總結
以上是生活随笔為你收集整理的视频识别成文字php,视频字幕和文字提取器-本地识别模型-人工智能文字识别源码交流...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年资料员-通用基础(资料员)考试
- 下一篇: php抓取html元素内容 采集网页