moffatman / chan

Flutter imageboard browser for iOS and Android
GNU General Public License v3.0
202 stars 8 forks source link

Need more solved captchas? #144

Open K1rakishou opened 2 years ago

K1rakishou commented 2 years ago

Hello. I noticed that you are collecting solved captchas and I'm assuming that's to train a NN model for future use. If I'm correct and you are planning to make it public then I could contribute you solved captchas to make the process faster (the same way you are doing it). I'm currently implementing captcha solver based on https://github.com/AUTOMATIC1111/4chan-captcha-solver model but it's not ideal.

moffatman commented 2 years ago

Hi @K1rakishou I tried using a neural network at the beginning, I made a program to generate fake captchas, but even with a data set of 100,000, I couldn't come up with a working model that would solve those generated captchas. So I gave up on neural networks, I'm not really an expert in that technique. I made the solver using classical techniques, so more data doesn't really help that much, and it is has kind of reached a plateau at ~65% accuracy.

I just rechecked https://github.com/AUTOMATIC1111/4chan-captcha-solver, from a first glance, it seems good and more accurate than my solver. What makes it not ideal? Too big for mobile devices?

By the way, I have ~34,000 captchas at the moment, let me know if it would help you in your effort, and I can provide them.

K1rakishou commented 2 years ago

Oh I see. Well I'm not an ML expert either, so more captchas won't help me.

This https://github.com/AUTOMATIC1111/4chan-captcha-solver model solves like 80-90% of captchas (I didn't measure it, just a feeling after using it for a couple of weeks) but the slider auto adjustment algorithm sometimes fails as well (which obviously makes the model fail too). You can manually adjust the slider and then solve it again and it works but it requires the user's input.

Too big for mobile devices?

The model itself is like 8 megabytes (tensorflow lite format, you can also enable optimizations when converting it so it will only take 2 megabytes but I don't know how it affects accuracy) however it uses some custom layers (?) and the default tensorflow lite does not support them so I had to include some special dependency with more layer implementations and it's size ended up being like ~40 megabytes just for one cpu architecture. I decided to extract that stuff into a separate apk and I use broadcast receivers to communicate between the main app and the solver app. https://github.com/K1rakishou/4chanCaptchaSolver

moffatman commented 2 years ago

the slider auto adjustment algorithm sometimes fails as well

You should try my slider algorithm here, I have only ever seen one failure in many months of use, and I think it's more efficient than the one in that repo as well. https://github.com/moffatman/chan/blob/b5ef7ef206584395e1151dee6060e50aa5f60619/lib/widgets/captcha_4chan.dart#L348 Basically, only compare the pixels of the image on the edge of the background cutouts.

How long does it take to execute the neural model on mobile?

K1rakishou commented 2 years ago

You should try my slider algorithm here

I will take a look at it but I think it's going to be very hard because the script is doing some additional captcha image transformations (it flips the pixels horizontally, rotates them and then draws everything in the middle of the canvas which also has specifically calculated size + adds some 0xeeeeee colored border around the image).

How long does it take to execute the neural model on mobile?

With no gpu and other kinds of accelerations (only cpu with 4 threads) and on an emulator in debug build it takes ~200ms. The slider auto adjustment however takes like 600-700ms. But all together it's less than a second which is pretty good. Guess it should be slightly better once the apk is built in release mode.

FrostyFridge commented 1 year ago

Hi, I'm interested in making my own attempt at one of these - Is there any chance I could get access to your captcha datasets @moffatman @K1rakishou ? Thanks!

moffatman commented 1 year ago

@FrostyFridge We can talk on discord, add me - Callum#2821

K1rakishou commented 1 year ago

@moffatman What's the endpoint for captcha collection? I would like to add support to donate captchas from KurobaEx. Seems like there are people willing to train a model with that data.

moffatman commented 1 year ago

@K1rakishou

You can POST images to https://captcha.chance.surf/kuroba.php, use multipart field "text" for the answer, and "image" for the image encoded as PNG.

K1rakishou commented 1 year ago

Thanks. I will try adding support for it tomorrow.

K1rakishou commented 1 year ago

For some reason it just keeps returning me status 400, "Missing file" error. I also tried to send it via postman but it gave me an error something like "Failed to move file".

    private suspend fun sendCaptcha(
        captchaBytes: ByteArray,
        captchaSolution: CaptchaSolution.ChallengeWithSolution
    ) {
        Logger.d(TAG, "sendCaptcha() captchaBytes: ${captchaBytes.size}, captchaSolution: ${captchaSolution}")

        val requestBody = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart(
                name = "text",
                value = captchaSolution.solution.uppercase()
            )
            .addFormDataPart(
                name = "image",
                filename = null,
                body = captchaBytes.toRequestBody(contentType = "image/png".toMediaType())
            )
            .build()

        val request = Request.Builder()
            .url("https://captcha.chance.surf/kuroba.php")
            .post(requestBody)
            .build()

        val response = proxiedOkHttpClient.okHttpClient().suspendCall(request)
        if (!response.isSuccessful) {
            val body = response.body?.string()?.take(256)
            Logger.e(TAG, "sendCaptcha() bad status code: ${response.code}, errorBody: '${body}'")
            return
        }

        Logger.d(TAG, "sendCaptcha() success")
    }
moffatman commented 1 year ago

If you try it again, I added more debug output to the response body, we can see what's missing in your request. I think you may need to provide some dummy filename like "image.png" instead of null.

K1rakishou commented 1 year ago

Ok let me try.

K1rakishou commented 1 year ago

Here is what I get, it gets cut off at the end.

sendCaptcha() bad status code: 500, errorBody: 'array(1) {
  ["text"]=>
  string(6) "NHRPHW"
}
array(1) {
  ["image"]=>
  array(5) {
    ["name"]=>
    string(9) "image.png"
    ["type"]=>
    string(9) "image/png"
    ["tmp_name"]=>
    string(14) "/tmp/phpeWdHbc"
    ["error"]=>
    int(0)
    ["size"'
moffatman commented 1 year ago

One more time, should be ok now

K1rakishou commented 1 year ago

Sent. Seems good on my end. Also, please check if the image has correct sizes because I might have fucked it up.

moffatman commented 1 year ago

Yep got it

VPDN4G_1691213544

K1rakishou commented 1 year ago

Nice.