Facing Issue when adding –
implementation ‘com.google.cloud:google-cloud-vision:3.44.0’ library in gradle facing error as shown in image
Gradle Error code below –
Caused by: java.lang.RuntimeException: Duplicate class com.google.api.Advice found in modules jetified-proto-google-common-protos-2.41.0 (com.google.api.grpc:proto-google-common-protos:2.41.0) and jetified-protolite-well-known-types-18.0.0-runtime (com.google.firebase:protolite-well-known-types:18.0.0)
2.Caused by: com.android.builder.merge.DuplicateRelativeFileException: 2 files found with path 'google/protobuf/empty.proto' from inputs:
3.Caused by: com.android.builder.merge.DuplicateRelativeFileException: 2 files found with path 'google/protobuf/empty.proto'.
I have tried to exclude module also but in that case i am unable to import required classes.
implementation (‘com.google.cloud:google-cloud-vision:3.44.0’){
exclude module : ‘proto-google-common-protos’
exclude module : ‘protolite-well-known-types’
exclude module : ‘protobuf-java’
exclude module : ‘protobuf-javalite’
}
Below is the function and screenshot where i am facing error –
private fun searchProduct(
segmentedImage: Bitmap,
x_min: Int,
y_min: Int,
x_max: Int,
y_max: Int,
half_sku: String
): FloatArray {
try {
// Load the credentials from the service account key JSON file
val credentials: GoogleCredentials =
GoogleCredentials.fromStream(context.assets.open("caratlane-v4-c3d8e96bfa95.json"))
val settings: ImageAnnotatorSettings = ImageAnnotatorSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build()
Log.d("LOOOOOOOOOOOOOOOOOLLLLLLLLL", credentials.toString())
try {
ImageAnnotatorClient.create(settings).use { client ->
// Convert the segmented image to ByteString
val baos = ByteArrayOutputStream()
segmentedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos)
val imgBytes =
ByteString.copyFrom(baos.toByteArray())
// Create an image request
val image: Image = Image.newBuilder()
.setContent(imgBytes)
.build()
val feature: Feature = Feature.newBuilder()
.setType(Feature.Type.PRODUCT_SEARCH)
.setMaxResults(10)
.build()
// BoundingPoly boundingPoly = BoundingPoly.newBuilder()
// .addVertices(Vertex.newBuilder().setX(x_min).setY(y_min).build())
// .addVertices(Vertex.newBuilder().setX(x_max).setY(y_min).build())
// .addVertices(Vertex.newBuilder().setX(x_max).setY(y_max).build())
// .addVertices(Vertex.newBuilder().setX(x_min).setY(y_max).build())
// .build();
val imageContext: ImageContext = ImageContext.newBuilder()
.setProductSearchParams(
ProductSearchParams.newBuilder()
.setProductSet(“projects/caratlane-v4/locations/asia-east1/productSets/cl-vs-prod-v6”)
.addProductCategories(“apparel-v2”)
.setFilter(“half_sku=$half_sku”) // .setBoundingPoly(boundingPoly)
.build()
)
.build()
val request: AnnotateImageRequest = AnnotateImageRequest.newBuilder()
.addFeatures(feature)
.setImage(image)
.setImageContext(imageContext)
.build()
// Perform the request
val responses: List<AnnotateImageResponse> =
client.batchAnnotateImages(listOf(request))
.getResponsesList()
for (response in responses) {
if (response.hasError()) {
Log.e(
"ProductSearch",
"Error: " + response.getError().getMessage()
)
}
// Parse and handle the response
val results: ProductSearchResults = response.getProductSearchResults()
var resultText = ""
val floatarr = FloatArray(10)
var i = 0
for (result in results.getResultsList()) {
resultText += result.getScore()
floatarr[i] = result.getScore()
i++
resultText += "n"
}
//result.setText("nVisual Search Scores:n$resultText")
return floatarr
}
}
} catch (e: Exception) {
Log.d("LOOOOOOOOOOOOOOOOOLLLLLLLLL", e.toString())
}
} catch (e: IOException) {
Log.e("ProductSearch", "Error searching product", e)
}
return FloatArray(0)
}