-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage_captioning using VIT Transformers.py
101 lines (74 loc) · 2.72 KB
/
Image_captioning using VIT Transformers.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import torch
import cv2
import numpy as np
from gtts import gTTS
import sounddevice as sd
import soundfile as sf
import tempfile
import warnings,logging
warnings.simplefilter('ignore')
logging.disable(logging.WARNING)
# Transformer and Pretrained Model
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, GPT2TokenizerFast
# Managing loading processsing
from tqdm import tqdm
# Assign available GPU
device = "cuda" if torch.cuda.is_available() else "cpu"
# ViT Encoder - Decoder Model
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning").to(device)
# Corresponding ViT Tokenizer
tokenizer = GPT2TokenizerFast.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
# Image processor
image_processor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
# Image inference
def get_caption(model, image_processor, tokenizer, image):
# Preprocessing the Image
img = image_processor(image, return_tensors = "pt").to(device)
# Generating captions
output = model.generate(**img)
# decode the output
caption = tokenizer.batch_decode(output, skip_special_tokens = True)[0]
return caption
def T_T_speech(text, Language='en'):
try:
# Generate speech
myobj = gTTS(text=text, lang=Language, slow=False)
# Save to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as fp:
myobj.write_to_fp(fp)
fp.seek(0)
# Read the audio file and play it
with sf.SoundFile(fp.name, 'r') as sound_file:
data = sound_file.read(dtype='int16')
sd.play(data, sound_file.samplerate)
sd.wait()
except Exception as e:
print(f"An error occurred: {e}")
# Function to detect scene change
def is_scene_change(current_frame, previous_frame, threshold = 1000):
# Calculate difference
difference = cv2.absdiff(current_frame, previous_frame)
non_zero_count = np.count_nonzero(difference)
return non_zero_count > threshold
# Initialize webcam
cap = cv2.VideoCapture(0)
ret, previous_frame = cap.read()
while True:
ret, current_frame = cap.read()
if not ret:
break
# Check for scene change
if is_scene_change(current_frame, previous_frame):
# Process the frame for captioning
caption = get_caption(model, image_processor, tokenizer, current_frame)
print(caption)
T_T_speech(caption)
previous_frame = current_frame.copy()
# Display the frame
cv2.imshow('Webcam Feed', current_frame)
# Exit on pressing 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close windows
cap.release()
cv2.destroyAllWindows()