forked from singhjaspreetb/Language-Translator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
video.py
64 lines (57 loc) · 2.06 KB
/
video.py
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
56
57
58
59
60
61
62
63
64
import cv2
import os, io
import tkinter as tk
from tkinter import filedialog
from google.cloud import vision_v1
from deep_translator import GoogleTranslator
from langdetect import detect
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r'api_json_file/translator-346404-9fadb5fe6cc1.json'
def langs():
lang_file=open("D:\Programming\AI\Project\language_support.txt","r")
target_language=input("enter the name of language(like: Hindi): ")
str=" "
while(str):
str=lang_file.readline()
l=str.split()
if target_language in l:
return l[len(l)-1]
target_lang=langs()
root =tk.Tk()
root.withdraw()
extensions=("*.webn", "*.mpg","*.mp2","*.mpeg","*.mp4","*.m4p","*.m4v","*.swf","*.wmv","*.avi")
filepath= filedialog.askopenfilename(title="Choose The Image", filetypes=(
("images", extensions), ("all files", "*.*")))
capture=cv2.VideoCapture(filepath)
len=int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
currentFrame=0
Frame_count=80
while(currentFrame<len):
ret, frame=capture.read()
name=str(currentFrame)+'.jpg'
cv2.imwrite('images/'+name,frame)
client = vision_v1.ImageAnnotatorClient()
file_name = os.path.join(os.path.dirname(__file__),
'images/'+name)
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision_v1.Image(content=content)
response = client.document_text_detection(image=image)
Text = response.full_text_annotation.text
if currentFrame==0:
print(Text)
souce_lang=detect(Text)
translated_text=GoogleTranslator(source=souce_lang, target=target_lang).translate(text=Text)
print(translated_text)
print()
elif Text==label:
pass
else:
print(Text)
souce_lang=detect(Text)
translated_text=GoogleTranslator(source=souce_lang, target=target_lang).translate(text=Text)
print(translated_text,'\n')
label=Text
currentFrame += Frame_count
os.remove('images/'+name)
capture.set(1,currentFrame)
cv2.destroyAllWindows()