Moved Image Search Component To Compilation

This commit is contained in:
devansh bawari 2021-07-15 14:32:53 +05:30
parent 3a57016884
commit e6668c442b
5 changed files with 174 additions and 145 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
{
"/js/velocity.js": "/js/velocity.js?id=c4402b16a156190bbbe1",
"/js/velocity.js": "/js/velocity.js?id=1fbd2262da4ff080d425",
"/css/velocity-admin.css": "/css/velocity-admin.css?id=4322502d80a0e4a0affd",
"/css/velocity.css": "/css/velocity.css?id=170046c99c8d10e332ef"
}

View File

@ -0,0 +1,164 @@
<template>
<div class="d-inline-block image-search-container" v-if="status == 'true'">
<label for="image-search-container">
<i class="icon camera-icon"></i>
<input
type="file"
class="d-none"
ref="image_search_input"
id="image-search-container"
v-on:change="loadLibrary()"
/>
<img
class="d-none"
id="uploaded-image-url"
:src="uploadedImageUrl"
alt=""
width="20"
height="20"
/>
</label>
</div>
</template>
<script>
export default {
props: ['status', 'uploadSrc', 'viewSrc', 'commonError', 'sizeLimitEror'],
data: function() {
return {
uploadedImageUrl: ''
};
},
methods: {
/**
* This method will dynamically load the scripts. Because image search library
* only used when someone clicks or interact with the image button. This will
* reduce some data usage for mobile user.
*/
loadLibrary: function() {
this.loadDynamicScript(
'https://cdn.jsdelivr.net/npm/@tensorflow/tfjs',
() => {
this.loadDynamicScript(
'https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet',
() => {
this.uploadImage();
}
);
}
);
},
/**
* This method will analyze the image and load the sets on the bases of trained model.
*/
uploadImage: function() {
var imageInput = this.$refs.image_search_input;
if (imageInput.files && imageInput.files[0]) {
if (imageInput.files[0].type.includes('image/')) {
if (imageInput.files[0].size <= 2000000) {
this.$root.showLoader();
var formData = new FormData();
formData.append('image', imageInput.files[0]);
axios
.post(this.uploadSrc, formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then(response => {
var net;
var self = this;
this.uploadedImageUrl = response.data;
async function app() {
var analysedResult = [];
var queryString = '';
net = await mobilenet.load();
const imgElement = document.getElementById(
'uploaded-image-url'
);
try {
const result = await net.classify(
imgElement
);
result.forEach(function(value) {
queryString = value.className.split(
','
);
if (queryString.length > 1) {
analysedResult = analysedResult.concat(
queryString
);
} else {
analysedResult.push(
queryString[0]
);
}
});
} catch (error) {
self.$root.hideLoader();
window.showAlert(
`alert-danger`,
this.__('shop.general.alert.error'),
this.commonError
);
}
localStorage.searchedImageUrl =
self.uploadedImageUrl;
queryString = localStorage.searched_terms = analysedResult.join(
'_'
);
self.$root.hideLoader();
window.location.href = `${self.viewSrc}?term=${queryString}&image-search=1`;
}
app();
})
.catch(() => {
this.$root.hideLoader();
window.showAlert(
`alert-danger`,
this.__('shop.general.alert.error'),
this.commonError
);
});
} else {
imageInput.value = '';
window.showAlert(
`alert-danger`,
this.__('shop.general.alert.error'),
this.sizeLimitEror
);
}
} else {
imageInput.value = '';
alert('Only images (.jpeg, .jpg, .png, ..) are allowed.');
}
}
}
}
};
</script>

View File

@ -74,6 +74,7 @@ Vue.component('carousel-component', require('./UI/components/carousel'));
Vue.component('child-sidebar', require('./UI/components/child-sidebar'));
Vue.component('card-list-header', require('./UI/components/card-header'));
Vue.component('magnify-image', require('./UI/components/image-magnifier'));
Vue.component('image-search-component', require('./UI/components/image-search'));
Vue.component('compare-component', require('./UI/components/product-compare'));
Vue.component('shimmer-component', require('./UI/components/shimmer-component'));
Vue.component('responsive-sidebar', require('./UI/components/responsive-sidebar'));

View File

@ -112,7 +112,13 @@
aria-label="Search"
v-model:value="inputVal" />
<image-search-component></image-search-component>
<image-search-component
status="{{core()->getConfigData('general.content.shop.image_search') == '1' ? 'true' : 'false'}}"
upload-src="{{ route('shop.image.search.upload') }}"
view-src="{{ route('shop.search.index') }}"
common-error="{{ __('shop::app.common.error') }}"
size-limit-error="{{ __('shop::app.common.image-upload-limit') }}">
</image-search-component>
<button class="btn" type="button" id="header-search-icon" aria-label="Search" @click="submitForm">
<i class="fs16 fw6 rango-search"></i>
@ -175,26 +181,6 @@
</div>
</script>
<script type="text/x-template" id="image-search-component-template">
<div class="d-inline-block image-search-container" v-if="image_search_status">
<label for="image-search-container">
<i class="icon camera-icon"></i>
<input
type="file"
class="d-none"
ref="image_search_input"
id="image-search-container"
v-on:change="loadLibrary()" />
<img
class="d-none"
id="uploaded-image-url"
:src="uploadedImageUrl" alt="" width="20" height="20" />
</label>
</div>
</script>
<script type="text/javascript">
(() => {
Vue.component('cart-btn', {
@ -362,128 +348,6 @@
}
}
});
Vue.component('image-search-component', {
template: '#image-search-component-template',
data: function() {
return {
uploadedImageUrl: '',
image_search_status: "{{core()->getConfigData('general.content.shop.image_search') == '1' ? 'true' : 'false'}}" == 'true'
}
},
methods: {
/**
* This method will dynamically load the scripts. Because image search library
* only used when someone clicks or interact with the image button. This will
* reduce some data usage for mobile user.
*/
loadLibrary: function() {
this.loadDynamicScript('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs', () => {
this.loadDynamicScript('https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet', () => {
this.uploadImage();
});
});
},
/**
* This method will analyze the image and load the sets on the bases of trained model.
*/
uploadImage: function() {
var imageInput = this.$refs.image_search_input;
if (imageInput.files && imageInput.files[0]) {
if (imageInput.files[0].type.includes('image/')) {
if (imageInput.files[0].size <= 2000000) {
this.$root.showLoader();
var formData = new FormData();
formData.append('image', imageInput.files[0]);
axios.post(
"{{ route('shop.image.search.upload') }}",
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then(response => {
var net;
var self = this;
this.uploadedImageUrl = response.data;
async function app() {
var analysedResult = [];
var queryString = '';
net = await mobilenet.load();
const imgElement = document.getElementById('uploaded-image-url');
try {
const result = await net.classify(imgElement);
result.forEach(function(value) {
queryString = value.className.split(',');
if (queryString.length > 1) {
analysedResult = analysedResult.concat(queryString)
} else {
analysedResult.push(queryString[0])
}
});
} catch (error) {
self.$root.hideLoader();
window.showAlert(
`alert-danger`,
this.__('shop.general.alert.error'),
"{{ __('shop::app.common.error') }}"
);
}
localStorage.searchedImageUrl = self.uploadedImageUrl;
queryString = localStorage.searched_terms = analysedResult.join('_');
self.$root.hideLoader();
window.location.href = "{{ route('shop.search.index') }}" + '?term=' + queryString + '&image-search=1';
}
app();
}).catch(() => {
this.$root.hideLoader();
window.showAlert(
`alert-danger`,
this.__('shop.general.alert.error'),
"{{ __('shop::app.common.error') }}"
);
});
} else {
imageInput.value = '';
window.showAlert(
`alert-danger`,
this.__('shop.general.alert.error'),
"{{ __('shop::app.common.image-upload-limit') }}"
);
}
} else {
imageInput.value = '';
alert('Only images (.jpeg, .jpg, .png, ..) are allowed.');
}
}
},
}
});
})();
</script>
@endpush