forked from Karthik110505/GRID_6.0_SMART_VISION
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpiry.py
More file actions
132 lines (107 loc) · 3.81 KB
/
expiry.py
File metadata and controls
132 lines (107 loc) · 3.81 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import cv2
from PIL import Image
import pytesseract
import re
from datetime import datetime
import random
def clean_date(date):
"""
Clean and refine the date to handle various formats like DD/MM/YYYY, MM/YYYY, or malformed years.
"""
current_year = datetime.now().year
current_year_short = int(str(current_year)[-2:])
max_year = current_year + 2 # Allow up to 2 years in the future
max_year_short = int(str(max_year)[-2:])
# Split the date by delimiters
date_parts = re.split(r'[-/]', date)
if len(date_parts) == 2:
# Handle MM/YYYY or MM/YY formats
month, year = date_parts
if len(year) == 2:
year = int(year)
if year > max_year_short:
return None
year = f"{current_year // 100}{year}"
day = random.randint(1, 28) # Assign a random day
return f"{day}/{month}/{year}"
if len(date_parts) == 3:
# Handle DD/MM/YYYY or malformed years
day, month, year = date_parts
if len(year) == 4:
year = int(year)
if year > max_year:
year = str(year)[:2] # Truncate malformed year
elif len(year) == 2:
year = int(year)
if year > max_year_short:
return None
year = f"{current_year // 100}{year}"
return f"{day}/{month}/{year}"
return None
def extract_dates_from_image(image_path):
"""
Extract and refine dates from an image.
"""
try:
text = pytesseract.image_to_string(Image.open(image_path))
print("Extracted text from image:", text)
date_pattern = r'\b(?:\d{1,2}[-/]\d{1,2}[-/]\d{2,4}|\d{1,2}[-/]\d{2,4})\b'
dates = re.findall(date_pattern, text)
refined_dates = set()
for date in dates:
cleaned_date = clean_date(date)
if cleaned_date:
refined_dates.add(cleaned_date)
return list(refined_dates)
except Exception as e:
print(f"Error processing the image: {e}")
return []
def extract_dates_from_video(video_path, skip_frames=5):
"""
Extract and refine dates from a video.
"""
try:
cap = cv2.VideoCapture(video_path)
frame_count = 0
dates_per_frame = []
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_count += 1
if frame_count % skip_frames != 0:
continue
pil_image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
text = pytesseract.image_to_string(pil_image)
print(f"Extracted text from frame {frame_count}: {text}")
date_pattern = r'\b(?:\d{1,2}[-/]\d{1,2}[-/]\d{2,4}|\d{1,2}[-/]\d{2,4})\b'
dates = re.findall(date_pattern, text)
refined_dates = set()
for date in dates:
cleaned_date = clean_date(date)
if cleaned_date:
refined_dates.add(cleaned_date)
dates_per_frame.append(list(refined_dates))
cap.release()
return dates_per_frame
except Exception as e:
print(f"Error processing the video: {e}")
return []
# Example usage
if __name__ == "__main__":
# For Image
image_path = "last2.png"
dates_image = extract_dates_from_image(image_path)
if dates_image:
print("Dates found in the image:", dates_image)
else:
print("No dates found in the image.")
# For Video
# video_path = "output3.mp4"
# dates_video = extract_dates_from_video(video_path)
# if any(dates_video):
# print("Dates found in the video frames:")
# for i, frame_dates in enumerate(dates_video):
# print(f"Frame {i + 1}: {frame_dates}")
# else:
# print("No dates found in the video.")