Skip to content

support msgpack#6

Open
Ehekatl wants to merge 2 commits intomasterfrom
develop-hyuwang
Open

support msgpack#6
Ehekatl wants to merge 2 commits intomasterfrom
develop-hyuwang

Conversation

@Ehekatl
Copy link
Contributor

@Ehekatl Ehekatl commented Apr 21, 2016

@Transfusion

Java Example for get bow_voc:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Base64;

import javax.net.ssl.HttpsURLConnection;

import com.google.gson.Gson;
import org.msgpack.MessagePack;

public class Main {

    MessagePack msgpack = new MessagePack();
    Gson gson = new Gson();

    public static void main(String[] args) throws Exception {

        Main http = new Main();

        http.sendGet();
    }

    private void sendGet() throws Exception {

        String url = "http://127.0.0.1:8000/vocabulary/";

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        byte[] bytes = Base64.getDecoder().decode(response.toString());
        Float[][] fff = msgpack.read(bytes, Float[][].class);
        System.out.println(Arrays.deepToString(fff));
    }
}

Python Example for search by histogram:

import time
import requests
import base64
import cv2
import numpy as np
import msgpack
from algolib.populator import Populator

url = 'http://host/vocabulary/'
s = base64.b64decode(requests.get(url).text)
voc = np.array(msgpack.unpackb(s), dtype=np.float32)

pop = Populator()
img = cv2.imread('test/6.jpg')
bow_hist = pop.bow_hist(img, voc)
color_hist = pop.color_hist(img)

bow_hist = base64.b64encode(msgpack.packb(bow_hist.tolist()))
color_hist = base64.b64encode(msgpack.packb(color_hist.tolist()))

url = 'http://host/images/'
args = {'limit': 20, 'method': 'chisqr_alt'}
data = {'bow_hist':bow_hist, 'color_hist':color_hist}
start_time = time.clock()
r = requests.post(url, data=data, params=args)
print(time.clock() - start_time, "seconds")
print(r.status_code)
print(r.text)

Python Example for search by file (sketched image with alpha channel):

import requests
import time

url = 'http://host/images/'
files = {'image': open('test/Drawing.png', 'rb')}
args = {'limit': 20, 'method': 'chisqr_alt', 'sketch': 'putanythinghere'}
start_time = time.clock()
r = requests.post(url, files=files, params=args)
print(time.clock() - start_time, "seconds")
print(r.status_code)
print(r.text)

@Ehekatl
Copy link
Contributor Author

Ehekatl commented Apr 21, 2016

@Transfusion
API Post result example:

[{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/07389.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/06273.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/07201.JPEG"},{"img_path":"http://7xnyxq.com1.z0.

glb.clouddn.com/01219.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/00287.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/05264.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/06288.JPEG"},{"img_p
ath":"http://7xnyxq.com1.z0.glb.clouddn.com/08774.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/00863.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/00059.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clou
ddn.com/08711.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/00424.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/08316.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/07062.JPEG"},{"img_path":"ht
tp://7xnyxq.com1.z0.glb.clouddn.com/03833.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/03966.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/07575.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/
03178.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/00745.JPEG"},{"img_path":"http://7xnyxq.com1.z0.glb.clouddn.com/07341.JPEG"}]

I disabled private url, now the image can have direct access, for example:
http://7xnyxq.com1.z0.glb.clouddn.com/03979.JPEG
img
If you need small size image:
http://7xnyxq.com1.z0.glb.clouddn.com/03979.JPEG-middle
img
http://7xnyxq.com1.z0.glb.clouddn.com/03979.JPEG-small
img
http://7xnyxq.com1.z0.glb.clouddn.com/03979.JPEG-tiny
img

@Ehekatl
Copy link
Contributor Author

Ehekatl commented Apr 21, 2016

Change:
Now the maximum number of results will be 100, you can add optional args to disable BoW compare

import requests
import time

url = 'http://host/images/'
files = {'image': open('test/Drawing.png', 'rb')}
args = {'limit': 20, 'method': 'chisqr_alt', 'sketch': 'putanythinghere', 'disableBoW': 'putanythinghere'}
start_time = time.clock()
r = requests.post(url, files=files, params=args)
print(time.clock() - start_time, "seconds")
print(r.status_code)
print(r.text)

As a result, the image search performance have great improvement, on local it deduce from 600+ ms to 100ms, on server it drop from 1400+ms to 400ms

Example result:
without BoW,
img
with BoW,
img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant