Skip to content

Commit 2271523

Browse files
Add files via upload
1 parent 4fe7104 commit 2271523

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed
+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 10,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"from PIL import Image\n",
10+
"import numpy as np\n",
11+
"import cv2"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 11,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"#pip install ImageHash (you need to do that in your Terminal)\n",
21+
"import imagehash\n",
22+
"#import scipy.spatial\n",
23+
"from scipy.spatial import distance"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 12,
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"name": "stdout",
33+
"output_type": "stream",
34+
"text": [
35+
"9a86372888939fbd ffe090a9c116dd32 e4eaabec9c919093\n"
36+
]
37+
}
38+
],
39+
"source": [
40+
"hash1 = imagehash.phash(Image.open('car1.png'))\n",
41+
"hash2 = imagehash.phash(Image.open('car2.png'))\n",
42+
"hash3 = imagehash.phash(Image.open('car3.png'))\n",
43+
"print (hash1, hash2, hash3)"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 13,
49+
"metadata": {},
50+
"outputs": [
51+
{
52+
"name": "stdout",
53+
"output_type": "stream",
54+
"text": [
55+
"9a86372888939fbd ffe090a9c116dd32 e4eaabec9c919093\n"
56+
]
57+
}
58+
],
59+
"source": [
60+
"hs1 = str(hash1)\n",
61+
"hs2 = str(hash2)\n",
62+
"hs3 = str(hash3)\n",
63+
"print (hs1, hs2, hs3)"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 14,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"# we are doing a string compare to find difference\n",
73+
"def hamming_distance(h1, h2):\n",
74+
" counter = 0\n",
75+
" for i in range(len(h1)):\n",
76+
" if h1[i] != h2[i]:\n",
77+
" counter += 1\n",
78+
" return counter"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 15,
84+
"metadata": {},
85+
"outputs": [
86+
{
87+
"name": "stdout",
88+
"output_type": "stream",
89+
"text": [
90+
"hs1-hs2 16\n",
91+
"hs1-hs3 14\n",
92+
"hs2-hs3 15\n",
93+
"hs2-hs2 0\n"
94+
]
95+
}
96+
],
97+
"source": [
98+
"#print (hamming_distance(hash1,hash2)) - this needs to be commented out to avoid error: object of type 'ImageHash' has no len()\n",
99+
"# so hash value is converted to string first\n",
100+
"\n",
101+
"print (\"hs1-hs2\",hamming_distance(hs1,hs2))\n",
102+
"print (\"hs1-hs3\",hamming_distance(hs1,hs3))\n",
103+
"print (\"hs2-hs3\",hamming_distance(hs2,hs3))\n",
104+
"print (\"hs2-hs2\",hamming_distance(hs2,hs2))"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 16,
110+
"metadata": {},
111+
"outputs": [
112+
{
113+
"name": "stdout",
114+
"output_type": "stream",
115+
"text": [
116+
"1.0\n"
117+
]
118+
}
119+
],
120+
"source": [
121+
"#note the distance.hamming function from scipy.spatial always return 1\n",
122+
"print(distance.hamming(hash1, hash2))"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 17,
128+
"metadata": {},
129+
"outputs": [
130+
{
131+
"name": "stdout",
132+
"output_type": "stream",
133+
"text": [
134+
"1.0\n"
135+
]
136+
}
137+
],
138+
"source": [
139+
"print(distance.hamming(hs1, hs2))"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": null,
145+
"metadata": {},
146+
"outputs": [],
147+
"source": []
148+
}
149+
],
150+
"metadata": {
151+
"kernelspec": {
152+
"display_name": "Python 3",
153+
"language": "python",
154+
"name": "python3"
155+
},
156+
"language_info": {
157+
"codemirror_mode": {
158+
"name": "ipython",
159+
"version": 3
160+
},
161+
"file_extension": ".py",
162+
"mimetype": "text/x-python",
163+
"name": "python",
164+
"nbconvert_exporter": "python",
165+
"pygments_lexer": "ipython3",
166+
"version": "3.7.3"
167+
}
168+
},
169+
"nbformat": 4,
170+
"nbformat_minor": 2
171+
}

Chapter01/car1.png

106 KB
Loading

Chapter01/car2.png

117 KB
Loading

Chapter01/car3.png

101 KB
Loading

0 commit comments

Comments
 (0)