-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathac.py
More file actions
137 lines (111 loc) · 4.33 KB
/
ac.py
File metadata and controls
137 lines (111 loc) · 4.33 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
132
133
134
135
136
137
import cv2
import streamlit as st
import numpy as np
from skimage.feature import hog
from sklearn.metrics.pairwise import cosine_similarity
import csv
import os
from scipy.stats import pearsonr
# Path to the CSV file to store HOG features
hog_features_file = "hog_features.csv"
def capture_image():
"""
Captures image from the pre-defined IP address.
Returns:
The captured frame.
"""
ip_address = " " # Change if needed
print(f"Attempting to open video stream from {ip_address}")
try:
cap = cv2.VideoCapture(ip_address)
if not cap.isOpened():
print("Error: Cannot open video stream.")
return None
while True:
ret, frame = cap.read()
if not ret:
print("Error reading video stream.")
return None
frame = cv2.resize(frame, (500, 500))
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord(' '):
cap.release()
cv2.destroyAllWindows()
return frame
except Exception as e:
print(f"Error capturing image: {e}")
return None
finally:
cap.release()
cv2.destroyAllWindows()
def compute_hog(image, pixels_per_cell=(30, 30), cells_per_block=(2, 2)):
"""
Computes HOG features for the given image.
"""
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
resized_image = cv2.resize(gray_image, (200, 100))
hog_features = hog(resized_image, pixels_per_cell=pixels_per_cell,
cells_per_block=cells_per_block, visualize=False, channel_axis=None)
return hog_features
def save_hog_features(account_number, hog_features):
"""
Saves the HOG features to a CSV file.
"""
with open(hog_features_file, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([account_number] + hog_features.tolist())
def load_hog_features(account_number):
"""
Loads the HOG features for a given account number from the CSV file.
"""
if not os.path.exists(hog_features_file):
return None
with open(hog_features_file, mode='r') as file:
reader = csv.reader(file)
for row in reader:
if row[0] == account_number:
return np.array(row[1:], dtype=np.float64)
return None
def compare_signatures(account_number, temp_image):
"""
Compares the captured image's HOG features with the stored HOG features.
"""
temp_image_hog = compute_hog(temp_image)
original_image_hog = load_hog_features(account_number)
if original_image_hog is not None:
# Existing user
score = cosine_similarity([temp_image_hog], [original_image_hog])[0][0]
correlation_coefficient, _ = pearsonr(temp_image_hog, original_image_hog)
return score, correlation_coefficient
else:
# New user
save_hog_features(account_number, temp_image_hog)
print(f"New user with account number {account_number}. HOG features saved.")
return None, None
def verify_signature(account_number, temp_image):
"""
Verifies the captured image against stored HOG features.
"""
similarity_score, correlation_coefficient = compare_signatures(account_number, temp_image)
if similarity_score is None:
st.write(f"New user with account number {account_number}. HOG features saved.")
else:
print(f"Similarity Score: {similarity_score:.2f}")
print(f"Correlation Coefficient: {correlation_coefficient:.2f}")
if correlation_coefficient > 0.75 and similarity_score > 0.86:
st.write("SIGNATURE IS ORIGINAL")
else:
st.write("SIGNATURE IS FORGED")
# Streamlit UI
st.title("Signature Verification")
account_number = st.text_input("Enter Account Number")
if st.button("Capture Image"):
if not account_number:
st.write("Please enter an account number.")
else:
temp_image = capture_image()
if temp_image is not None:
st.image(temp_image, caption="Captured Image", channels="BGR")
verify_signature(account_number, temp_image)
else:
st.write("Failed to capture image. Please try again.")