goCaptcha/README.md
README.mdRaw
1# goCaptcha
2captcha server, with own datasets, to train own machine learning AI
3
4### How to use?
5#### Frontend
6Insert this lines in the html file:
7```html
8 <link rel="stylesheet" href="goCaptcha.css">
9
10 <div id="goCaptcha"></div>
11
12 <script>//define the url where is placed the server of goCaptcha
13 var goCaptchaURL = "http://127.0.0.1:3025";
14 </script>
15 <script src="goCaptcha.js"></script>
16```
17
18It will place the goCaptcha box in the div:
19
20
21
22#### Backend
23- Put dataset images in the folder 'imgs'.
24- Run MongoDB.
25- Go to the folder /goCaptcha, and run:
26```
27./goCaptcha
28```
29It will show:
30```
31user@laptop:~/goCaptcha$ ./goCaptcha
32 goCaptcha started
33 dataset read
34 num of dataset categories: 4
35 server running
36 port: 3025
37```
38
39
40### How to make the petitions?
41
421. Get the captcha:
43```
44GET/ 127.0.0.1:3025/captcha
45```
46Server response:
47```json
48{
49 "id": "881c6083-0643-4d1c-9987-f8cc5bb9d5b1",
50 "imgs": [
51 "7cf6f630-e78f-469c-85dd-2d677996fea1.png",
52 "d4014318-f875-4b42-b704-4f5bf5e5e00c.png",
53 "2dd69b44-903d-4e78-bb7b-f8b07877c9e5.png",
54 "2954fc38-819d-40c9-ae3e-7b6fbb68ddbe.png",
55 "b060f58a-d44b-4e05-b466-92aa801a2aa1.png",
56 "1b838c46-b784-471e-b143-48be058c39a7.png"
57 ],
58 "question": "leopard",
59 "date": ""
60}
61```
62
632. User selects the images that fit in the 'question' parameter
64(in this case, 'leopard')
65
663. Post the answer. The answer contains the CaptchaId, and an array with the selected images
67```
68POST/ 127.0.0.1:3025/answer
69```
70Post example:
71```json
72{
73 "captchaid": "881c6083-0643-4d1c-9987-f8cc5bb9d5b1",
74 "selection": [0,0,0,0,1,1]
75}
76```
77Server response:
78```
79true
80```
81
82### How this works?
83
84###### Server reads dataset
85First, server reads all dataset. Dataset is a directory with subdirectories, where each subdirectory contains images of one element.
86
87For example:
88```
89imgs/
90 leopard/
91 img01.png
92 img02.png
93 img03.png
94 ...
95 laptop/
96 img01.png
97 img02.png
98 ...
99 house/
100 img01.png
101 img02.png
102 ...
103```
104Then, stores all the filenames corresponding to each subdirectory. So, we have each image and to which element category is (the name of subdirectory).
105
106
107###### Server generates captcha
108When server recieves a GET /captcha, generates a captcha, getting random images from the dataset.
109
110For each captcha generated, generates two mongodb models:
111```json
112Captcha Model
113{
114 "id" : "881c6083-0643-4d1c-9987-f8cc5bb9d5b1",
115 "imgs" : [
116 "7cf6f630-e78f-469c-85dd-2d677996fea1.png",
117 "d4014318-f875-4b42-b704-4f5bf5e5e00c.png",
118 "2dd69b44-903d-4e78-bb7b-f8b07877c9e5.png",
119 "2954fc38-819d-40c9-ae3e-7b6fbb68ddbe.png",
120 "b060f58a-d44b-4e05-b466-92aa801a2aa1.png",
121 "1b838c46-b784-471e-b143-48be058c39a7.png"
122 ],
123 "question" : "leopard"
124}
125```
126
127```json
128CaptchaSolution Model
129{
130 "id" : "881c6083-0643-4d1c-9987-f8cc5bb9d5b1",
131 "imgs" : [
132 "image_0022.jpg",
133 "image_0006.jpg",
134 "image_0050.jpg",
135 "image_0028.jpg",
136 "image_0119.jpg",
137 "image_0092.jpg"
138 ],
139 "imgssolution" : [
140 "camera",
141 "camera",
142 "laptop",
143 "crocodile",
144 "leopard",
145 "leopard"
146 ],
147 "question" : "leopard"
148}
149```
150Both models are stored in the MongoDB.
151
152The Captcha Model 'imgs' parameter, are UUIDs generated to set 'random' names to images. The server stores into MongoDB the relation between the 'random' name of each image and the real path of the image:
153```json
154{
155 "captchaid" : "881c6083-0643-4d1c-9987-f8cc5bb9d5b1",
156 "real" : "leopard/image_0092.jpg",
157 "fake" : "1b838c46-b784-471e-b143-48be058c39a7.png"
158}
159```
160When the server recieves a petition to get an image, recieves the petition with the fake image name, then, gets the real path of the image, gets it and serves the image content under the fake image name:
161```
162127.0.0.1:3025/image/1b838c46-b784-471e-b143-48be058c39a7.png
163```
164
165Captcha Model contains the captcha that server returns to the petition. And CaptchaSolution contains the solution of the captcha. Both have the same Id.
166
167
168###### Server validates captcha
169When server recieves POST /answer, gets the answer, search for the CaptchaSolution based on the CaptchaId in the MongoDB, and then compares the answer 'selection' parameter with the CaptchaSolution.
170
171If the selection is correct, returns 'true', if the selection is not correct, returns 'false'.