4NK_IA_front/test-backend-direct.cjs

156 lines
4.3 KiB
JavaScript

#!/usr/bin/env node
/**
* Test direct du backend pour analyser les images CNI
* sans configuration spécifique, juste l'extraction OCR normale
*/
const { createWorker } = require('tesseract.js')
const path = require('path')
const fs = require('fs')
console.log('🔍 Test direct backend - Analyse OCR normale')
console.log('=============================================')
const imagePath = path.join(__dirname, 'test-files', 'IMG_20250902_162210.jpg')
// Vérifier que l'image existe
if (!fs.existsSync(imagePath)) {
console.error('❌ Image non trouvée:', imagePath)
process.exit(1)
}
console.log('📸 Image trouvée:', path.basename(imagePath))
// Fonction d'extraction OCR simple
async function extractTextFromImage(imagePath) {
console.log("\n🚀 Démarrage de l'extraction OCR...")
const worker = await createWorker('fra+eng')
try {
console.log('⏳ Extraction en cours...')
const {
data: { text },
} = await worker.recognize(imagePath)
console.log('\n📄 TEXTE EXTRAIT:')
console.log('='.repeat(50))
console.log(text)
console.log('='.repeat(50))
return text
} catch (error) {
console.error('❌ Erreur OCR:', error.message)
return null
} finally {
await worker.terminate()
}
}
// Fonction d'analyse simple du texte
function analyzeText(text) {
console.log('\n🔍 ANALYSE DU TEXTE:')
console.log('='.repeat(50))
if (!text) {
console.log('❌ Aucun texte à analyser')
return
}
console.log(`📊 Longueur du texte: ${text.length} caractères`)
// Recherche de noms (patterns généraux)
const namePatterns = [
/([A-Z][a-zà-öø-ÿ'\-]+\s+[A-Z][a-zà-öø-ÿ'\-]+)/g, // Prénom Nom
/([A-Z][A-ZÀ-ÖØ-öø-ÿ\s\-']{2,30})/g, // Noms en majuscules
]
const foundNames = new Set()
for (const pattern of namePatterns) {
for (const match of text.matchAll(pattern)) {
const name = match[1].trim()
if (name.length > 3) {
foundNames.add(name)
}
}
}
console.log(`👥 Noms détectés: ${foundNames.size}`)
foundNames.forEach((name, index) => {
console.log(` ${index + 1}. "${name}"`)
})
// Recherche de numéros CNI (2 lettres + 6 chiffres)
const cniPattern = /([A-Z]{2}\d{6})/g
const cniNumbers = []
for (const match of text.matchAll(cniPattern)) {
cniNumbers.push(match[1])
}
console.log(`🆔 Numéros CNI détectés: ${cniNumbers.length}`)
cniNumbers.forEach((cni, index) => {
console.log(` ${index + 1}. "${cni}"`)
})
// Recherche spécifique pour NICOLAS et CANTU
console.log('\n🔍 RECHERCHE SPÉCIFIQUE:')
console.log('='.repeat(50))
const hasNicolas = /nicolas/i.test(text)
const hasCantu = /cantu/i.test(text)
const hasNicolasCantu = /nicolas.*cantu|cantu.*nicolas/i.test(text)
console.log(`🔍 "NICOLAS" trouvé: ${hasNicolas ? '✅ OUI' : '❌ NON'}`)
console.log(`🔍 "CANTU" trouvé: ${hasCantu ? '✅ OUI' : '❌ NON'}`)
console.log(`🔍 "NICOLAS CANTU" ensemble: ${hasNicolasCantu ? '✅ OUI' : '❌ NON'}`)
if (hasNicolas || hasCantu) {
console.log('\n📝 Contexte trouvé:')
const lines = text.split('\n')
lines.forEach((line, index) => {
if (/nicolas|cantu/i.test(line)) {
console.log(` Ligne ${index + 1}: "${line.trim()}"`)
}
})
}
// Recherche de mots-clés CNI
const hasCNIKeywords = /carte\s+nationale\s+d'identité|cni|mrz|identite/i.test(text)
console.log(`📋 Mots-clés CNI détectés: ${hasCNIKeywords ? '✅ OUI' : '❌ NON'}`)
return {
text,
names: Array.from(foundNames),
cniNumbers,
hasNicolas,
hasCantu,
hasNicolasCantu,
hasCNIKeywords,
}
}
// Fonction principale
async function main() {
try {
const text = await extractTextFromImage(imagePath)
const results = analyzeText(text)
console.log('\n🎯 RÉSULTATS FINAUX:')
console.log('='.repeat(50))
console.log(`📄 Texte extrait: ${results ? '✅' : '❌'}`)
console.log(`👥 Noms trouvés: ${results?.names.length || 0}`)
console.log(`🆔 CNI trouvés: ${results?.cniNumbers.length || 0}`)
console.log(`🔍 NICOLAS: ${results?.hasNicolas ? '✅' : '❌'}`)
console.log(`🔍 CANTU: ${results?.hasCantu ? '✅' : '❌'}`)
console.log(`📋 Type CNI: ${results?.hasCNIKeywords ? '✅' : '❌'}`)
} catch (error) {
console.error('❌ Erreur:', error.message)
}
}
// Exécuter
main()