@nyris/nyris-webapp 0.3.2 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/asset-manifest.json +8 -8
- package/build/index.html +1 -1
- package/build/js/test.js +14 -20
- package/build/{precache-manifest.8f85a4fff2063a7498a608ec69549bb1.js → precache-manifest.bbb31f2ce7710d7eb3175b1b48241d24.js} +9 -9
- package/build/service-worker.js +1 -1
- package/build/static/js/2.4a55bc61.chunk.js +3 -0
- package/build/static/js/{2.efc0ab83.chunk.js.LICENSE.txt → 2.4a55bc61.chunk.js.LICENSE.txt} +0 -9
- package/build/static/js/2.4a55bc61.chunk.js.map +1 -0
- package/build/static/js/main.2660f94a.chunk.js +2 -0
- package/build/static/js/main.2660f94a.chunk.js.map +1 -0
- package/package.json +3 -3
- package/public/js/test.js +14 -20
- package/src/App.tsx +3 -7
- package/src/Store/Nyris.ts +7 -7
- package/src/Store/Search.ts +48 -74
- package/src/Store/Store.ts +12 -5
- package/src/components/CustomHits/index.tsx +57 -0
- package/src/components/DragDropFile.tsx +27 -23
- package/src/components/ExampleImages.tsx +2 -2
- package/src/components/Feedback.tsx +2 -2
- package/src/components/Footer.tsx +1 -1
- package/src/components/HeaderMd.tsx +4 -5
- package/src/index.tsx +2 -1
- package/src/modules/LandingPage/{indexApp.tsx → App.tsx} +52 -208
- package/src/modules/LandingPage/{indexAppMD.tsx → AppMD.tsx} +52 -147
- package/src/modules/LandingPage/index.tsx +169 -0
- package/src/modules/LandingPage/indexNewVersion.tsx +14 -21
- package/src/modules/LandingPage/propsType.ts +43 -0
- package/src/page/result/index.tsx +51 -106
- package/src/services/Feedback.ts +46 -47
- package/src/services/image.ts +28 -98
- package/src/services/session.ts +13 -16
- package/src/services/types.ts +2 -48
- package/src/types.ts +12 -8
- package/build/static/js/2.efc0ab83.chunk.js +0 -3
- package/build/static/js/2.efc0ab83.chunk.js.map +0 -1
- package/build/static/js/main.3bfed050.chunk.js +0 -2
- package/build/static/js/main.3bfed050.chunk.js.map +0 -1
- package/src/App.test.tsx +0 -49
- package/src/Store/common.d.ts +0 -10
- package/src/Store/epics/feedback.ts +0 -59
- package/src/Store/epics/types.ts +0 -12
- package/src/components/preview/preview.tsx +0 -433
- package/src/services/findByImage.ts +0 -24
- package/src/services/findRegionsCustom.ts +0 -212
- package/src/services/nyris.ts +0 -123
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ImageSearchOptions,
|
|
3
|
-
Region,
|
|
4
|
-
Result,
|
|
5
|
-
SearchServiceSettings,
|
|
6
|
-
} from "./types";
|
|
7
|
-
// import {canvasToJpgBlob, getElementSize, getThumbSizeArea, toCanvas} from "./nyris";
|
|
8
|
-
import axios, { AxiosInstance } from "axios";
|
|
9
|
-
import {
|
|
10
|
-
canvasToJpgBlob,
|
|
11
|
-
getElementSize,
|
|
12
|
-
getThumbSizeArea,
|
|
13
|
-
toCanvas,
|
|
14
|
-
} from "./nyris";
|
|
15
|
-
|
|
16
|
-
export interface RegionData {
|
|
17
|
-
rect: {
|
|
18
|
-
x: number;
|
|
19
|
-
y: number;
|
|
20
|
-
w: number;
|
|
21
|
-
h: number;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
interface SearchResult {
|
|
26
|
-
results: Result[];
|
|
27
|
-
requestId: string;
|
|
28
|
-
categoryPredictions: any[];
|
|
29
|
-
codes: any[];
|
|
30
|
-
duration: number;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export default class NyrisAPICT {
|
|
34
|
-
private readonly httpClient: AxiosInstance;
|
|
35
|
-
private readonly imageMatchingUrl: string;
|
|
36
|
-
private readonly regionProposalUrl: string;
|
|
37
|
-
private readonly responseFormat: string;
|
|
38
|
-
private imageMatchingUrlBySku: string;
|
|
39
|
-
private imageMatchingSubmitManualUrl: string;
|
|
40
|
-
private feedbackUrl: string;
|
|
41
|
-
|
|
42
|
-
constructor(private settings: SearchServiceSettings) {
|
|
43
|
-
this.httpClient = axios.create();
|
|
44
|
-
this.imageMatchingUrl =
|
|
45
|
-
this.settings.imageMatchingUrl || "https://api.nyris.io/find/v1";
|
|
46
|
-
this.imageMatchingUrlBySku =
|
|
47
|
-
this.settings.imageMatchingUrlBySku ||
|
|
48
|
-
"https://api.nyris.io/recommend/v1/";
|
|
49
|
-
this.imageMatchingSubmitManualUrl =
|
|
50
|
-
this.settings.imageMatchingSubmitManualUrl ||
|
|
51
|
-
"https://api.nyris.io/find/v1/manual/";
|
|
52
|
-
this.feedbackUrl =
|
|
53
|
-
this.settings.feedbackUrl || "https://api.nyris.io/feedback/v1/";
|
|
54
|
-
this.regionProposalUrl =
|
|
55
|
-
this.settings.regionProposalUrl ||
|
|
56
|
-
"https://api.nyris.io/find/v1/regions/";
|
|
57
|
-
this.responseFormat =
|
|
58
|
-
this.settings.responseFormat || "application/offers.nyris+json";
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Find significant sections in the image.
|
|
63
|
-
* @param canvas Canvas, video or image to search with.
|
|
64
|
-
* @param options See [[ImageSearchOptions]].
|
|
65
|
-
* @returns A list of regions, see [[Region]].
|
|
66
|
-
*/
|
|
67
|
-
async findRegions(
|
|
68
|
-
canvas: HTMLCanvasElement | HTMLVideoElement | HTMLImageElement,
|
|
69
|
-
options: ImageSearchOptions
|
|
70
|
-
): Promise<Region[]> {
|
|
71
|
-
let [origW, origH] = getElementSize(canvas);
|
|
72
|
-
let { w: scaledW, h: scaledH } = getThumbSizeArea(
|
|
73
|
-
options.maxWidth,
|
|
74
|
-
options.maxHeight,
|
|
75
|
-
origW,
|
|
76
|
-
origH
|
|
77
|
-
);
|
|
78
|
-
let resizedCroppedCanvas = toCanvas(canvas, { w: scaledW, h: scaledH });
|
|
79
|
-
let blob = await canvasToJpgBlob(resizedCroppedCanvas, options.jpegQuality);
|
|
80
|
-
|
|
81
|
-
const headers = {
|
|
82
|
-
"Content-Type": "image/jpeg",
|
|
83
|
-
"X-Api-Key": this.settings.apiKey,
|
|
84
|
-
};
|
|
85
|
-
let response = await this.httpClient.request<any[]>({
|
|
86
|
-
method: "POST",
|
|
87
|
-
url: this.regionProposalUrl,
|
|
88
|
-
data: blob,
|
|
89
|
-
headers,
|
|
90
|
-
});
|
|
91
|
-
let regions: any[] = response.data;
|
|
92
|
-
return regions.map((r) => ({
|
|
93
|
-
className: r.className,
|
|
94
|
-
confidence: r.confidence,
|
|
95
|
-
x1: r.region.left / scaledW,
|
|
96
|
-
x2: r.region.right / scaledW,
|
|
97
|
-
y1: r.region.top / scaledH,
|
|
98
|
-
y2: r.region.bottom / scaledH,
|
|
99
|
-
}));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
private async prepareImage(
|
|
103
|
-
canvas: HTMLCanvasElement | HTMLImageElement | HTMLVideoElement,
|
|
104
|
-
options: ImageSearchOptions
|
|
105
|
-
): Promise<{ bytes: Blob; region?: RegionData }> {
|
|
106
|
-
let [w, h] = getElementSize(canvas);
|
|
107
|
-
let crop = options.crop
|
|
108
|
-
? options.crop
|
|
109
|
-
: {
|
|
110
|
-
x: 0,
|
|
111
|
-
y: 0,
|
|
112
|
-
w: w,
|
|
113
|
-
h: h,
|
|
114
|
-
};
|
|
115
|
-
let region: RegionData | undefined = undefined;
|
|
116
|
-
if (options.crop) {
|
|
117
|
-
region = {
|
|
118
|
-
rect: {
|
|
119
|
-
w: Math.min(1, crop.w / w),
|
|
120
|
-
h: Math.min(1, crop.h / h),
|
|
121
|
-
x: Math.min(1, crop.x / w),
|
|
122
|
-
y: Math.min(1, crop.y / h),
|
|
123
|
-
},
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
let scaledSize = getThumbSizeArea(
|
|
127
|
-
options.maxWidth,
|
|
128
|
-
options.maxHeight,
|
|
129
|
-
crop.w,
|
|
130
|
-
crop.h
|
|
131
|
-
);
|
|
132
|
-
let resizedCroppedCanvas = toCanvas(canvas, scaledSize, undefined, crop);
|
|
133
|
-
let bytes = await canvasToJpgBlob(
|
|
134
|
-
resizedCroppedCanvas,
|
|
135
|
-
options.jpegQuality
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
return { bytes, region };
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async findByImage(
|
|
142
|
-
canvas: HTMLCanvasElement | HTMLImageElement | HTMLVideoElement,
|
|
143
|
-
options: ImageSearchOptions
|
|
144
|
-
): Promise<SearchResult> {
|
|
145
|
-
const image = await this.prepareImage(canvas, options);
|
|
146
|
-
|
|
147
|
-
if (this.settings.customSearchRequest)
|
|
148
|
-
return this.settings.customSearchRequest(image.bytes, this.httpClient); // TODO check if the interface is ok for hooks
|
|
149
|
-
|
|
150
|
-
let headers: any = {
|
|
151
|
-
"Content-Type": "image/jpeg",
|
|
152
|
-
"X-Api-Key": this.settings.apiKey,
|
|
153
|
-
"Accept-Language": "de,*;q=0.5",
|
|
154
|
-
Accept: this.responseFormat,
|
|
155
|
-
};
|
|
156
|
-
const xOptions = [];
|
|
157
|
-
if (this.settings.xOptions) xOptions.push(this.settings.xOptions as string);
|
|
158
|
-
if (options.useRecommendations) xOptions.push("+recommendations");
|
|
159
|
-
if (xOptions.length > 0) headers["X-Options"] = xOptions.join(" ");
|
|
160
|
-
let params = options.geoLocation
|
|
161
|
-
? {
|
|
162
|
-
lat: options.geoLocation.lat.toString(),
|
|
163
|
-
lon: options.geoLocation.lon.toString(),
|
|
164
|
-
dist: options.geoLocation.dist.toString(),
|
|
165
|
-
}
|
|
166
|
-
: {};
|
|
167
|
-
// console.log("p", params, image.bytes);
|
|
168
|
-
let res: any = await this.httpClient.request<any>({
|
|
169
|
-
method: "POST",
|
|
170
|
-
url: this.imageMatchingUrl,
|
|
171
|
-
data: image.bytes,
|
|
172
|
-
params,
|
|
173
|
-
headers,
|
|
174
|
-
responseType: "json",
|
|
175
|
-
});
|
|
176
|
-
// console.log(res);
|
|
177
|
-
const categoryPredictions = Object.entries(
|
|
178
|
-
res.data.predicted_category || {}
|
|
179
|
-
)
|
|
180
|
-
.map(([name, score]) => ({
|
|
181
|
-
name: name,
|
|
182
|
-
score: score as number,
|
|
183
|
-
}))
|
|
184
|
-
.sort((a, b) => b.score - a.score);
|
|
185
|
-
let codes = res.data.barcodes || [];
|
|
186
|
-
|
|
187
|
-
let responseData = this.settings.responseHook
|
|
188
|
-
? this.settings.responseHook(res.data)
|
|
189
|
-
: res.data;
|
|
190
|
-
|
|
191
|
-
let results: Result[] = responseData?.results.map(
|
|
192
|
-
(r: Result, i: number) => ({
|
|
193
|
-
...r,
|
|
194
|
-
position: i,
|
|
195
|
-
})
|
|
196
|
-
);
|
|
197
|
-
const requestId = res.headers["x-matching-request"];
|
|
198
|
-
const duration = res.data.durationSeconds;
|
|
199
|
-
return { results, requestId, duration, categoryPredictions, codes };
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async createSession(): Promise<{ data: any }> {
|
|
203
|
-
let headers: any = {
|
|
204
|
-
"X-Api-Key": this.settings.apiKey,
|
|
205
|
-
};
|
|
206
|
-
return await this.httpClient.request({
|
|
207
|
-
method: "POST",
|
|
208
|
-
url: `${this.imageMatchingUrl}/session`,
|
|
209
|
-
headers,
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
}
|
package/src/services/nyris.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import loadImage from "blueimp-load-image";
|
|
2
|
-
import { Crop, WH } from "./types";
|
|
3
|
-
|
|
4
|
-
export function getElementSize(
|
|
5
|
-
elem: HTMLImageElement | HTMLVideoElement | HTMLCanvasElement
|
|
6
|
-
) {
|
|
7
|
-
const img = elem as HTMLImageElement;
|
|
8
|
-
const video = elem as HTMLVideoElement;
|
|
9
|
-
return [
|
|
10
|
-
img.naturalWidth || video.videoWidth || elem.width,
|
|
11
|
-
img.naturalHeight || video.videoHeight || elem.height,
|
|
12
|
-
];
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function getThumbSizeArea(
|
|
16
|
-
maxWidth: number,
|
|
17
|
-
maxHeight: number,
|
|
18
|
-
originalWidth: number,
|
|
19
|
-
originalHeight: number
|
|
20
|
-
): WH {
|
|
21
|
-
const targetArea = maxWidth * maxHeight;
|
|
22
|
-
|
|
23
|
-
const aspectRatio = originalWidth / originalHeight;
|
|
24
|
-
const width = Math.sqrt(targetArea * aspectRatio);
|
|
25
|
-
return {
|
|
26
|
-
w: width,
|
|
27
|
-
h: width / aspectRatio,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// TODO get rid of crop type
|
|
32
|
-
export function toCanvas(
|
|
33
|
-
elem: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement,
|
|
34
|
-
newSize?: WH,
|
|
35
|
-
canvas?: HTMLCanvasElement,
|
|
36
|
-
crop?: Crop
|
|
37
|
-
): HTMLCanvasElement {
|
|
38
|
-
let [ow, oh] = getElementSize(elem);
|
|
39
|
-
if (!crop) {
|
|
40
|
-
crop = {
|
|
41
|
-
x: 0,
|
|
42
|
-
y: 0,
|
|
43
|
-
w: ow,
|
|
44
|
-
h: oh,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
const sx = crop.x;
|
|
48
|
-
const sy = crop.y;
|
|
49
|
-
const sw = crop.w;
|
|
50
|
-
const sh = crop.h;
|
|
51
|
-
|
|
52
|
-
const dw = (newSize && newSize.w) || ow;
|
|
53
|
-
const dh = (newSize && newSize.h) || oh;
|
|
54
|
-
|
|
55
|
-
if (!canvas) canvas = document.createElement("canvas");
|
|
56
|
-
|
|
57
|
-
canvas.width = dw;
|
|
58
|
-
canvas.height = dh;
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
canvas.getContext("2d").drawImage(
|
|
61
|
-
elem,
|
|
62
|
-
sx,
|
|
63
|
-
sy,
|
|
64
|
-
sw,
|
|
65
|
-
sh,
|
|
66
|
-
0,
|
|
67
|
-
0, // dx dy
|
|
68
|
-
dw,
|
|
69
|
-
dh
|
|
70
|
-
);
|
|
71
|
-
return canvas;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function canvasToJpgBlob(
|
|
75
|
-
canvas: HTMLCanvasElement,
|
|
76
|
-
quality: number
|
|
77
|
-
): Promise<Blob> {
|
|
78
|
-
return new Promise((resolve, reject) => {
|
|
79
|
-
canvas.toBlob(
|
|
80
|
-
(blob) => {
|
|
81
|
-
if (blob) {
|
|
82
|
-
resolve(blob);
|
|
83
|
-
} else {
|
|
84
|
-
reject();
|
|
85
|
-
}
|
|
86
|
-
},
|
|
87
|
-
"image/jpeg",
|
|
88
|
-
quality
|
|
89
|
-
);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export const rectToCrop = ({ x1, x2, y1, y2 }: any): Crop => ({
|
|
94
|
-
x: x1,
|
|
95
|
-
y: y1,
|
|
96
|
-
w: x2 - x1,
|
|
97
|
-
h: y2 - y1,
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
export function fileOrBlobToCanvas(
|
|
101
|
-
file: File | string
|
|
102
|
-
): Promise<HTMLCanvasElement> {
|
|
103
|
-
return new Promise((resolve, reject) => {
|
|
104
|
-
// File can also be an image element
|
|
105
|
-
loadImage(
|
|
106
|
-
file,
|
|
107
|
-
(data) => {
|
|
108
|
-
const c = data as HTMLCanvasElement;
|
|
109
|
-
if (c) {
|
|
110
|
-
resolve(c);
|
|
111
|
-
} else {
|
|
112
|
-
reject();
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
{
|
|
116
|
-
canvas: true,
|
|
117
|
-
orientation: true,
|
|
118
|
-
crossOrigin: "anonymous",
|
|
119
|
-
}
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
});
|
|
123
|
-
}
|