-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
33 lines (28 loc) · 1010 Bytes
/
preprocess.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
import cv2
import numpy as np
import piexif
import nltk
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
nltk.download('punkt')
def preprocess_image(image_path):
"""Preprocess the image for model input."""
image = cv2.imread(image_path)
image = cv2.resize(image, (224, 224))
image = image.astype('float32') / 255.0
return image
def extract_metadata(image_path):
"""Extract metadata from the image."""
exif_data = piexif.load(image_path)
return exif_data
def preprocess_text(text):
"""Tokenize and prepare text data."""
return ' '.join(word_tokenize(text.lower()))
def vectorize_text(texts, vectorizer=None):
"""Convert texts into numerical vectors."""
if vectorizer is None:
vectorizer = TfidfVectorizer(ngram_range=(1, 2), max_df=0.8, min_df=5)
text_vectors = vectorizer.fit_transform(texts)
else:
text_vectors = vectorizer.transform(texts)
return text_vectors, vectorizer