joermd commited on
Commit
e515a9f
·
verified ·
1 Parent(s): 9a1ffcf

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +65 -41
index.html CHANGED
@@ -2,7 +2,7 @@
2
  <html lang="ar" dir="rtl">
3
  <head>
4
  <meta charset="UTF-8">
5
- <title>تحويل صورة إلى نص - OCR</title>
6
  <style>
7
  body { font-family: Arial; margin: 40px; }
8
  label, button { display: block; margin-top: 15px; }
@@ -10,60 +10,84 @@
10
  </style>
11
  </head>
12
  <body>
13
- <h2>تحويل صورة إلى نص (OCR)</h2>
14
- <form id="ocr-form">
15
- <label>اختر صورة:
16
- <input type="file" id="image-input" accept="image/*" required>
17
  </label>
18
- <button type="submit">رفع وتحويل</button>
 
 
 
19
  </form>
20
  <div id="result"></div>
21
 
22
  <script>
23
- document.getElementById('ocr-form').addEventListener('submit', async function(event) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  event.preventDefault();
25
  const resultDiv = document.getElementById('result');
26
- resultDiv.textContent = "جاري التحويل...";
 
 
27
 
 
28
  const fileInput = document.getElementById('image-input');
29
- if (!fileInput.files.length) {
30
- resultDiv.textContent = "اختر صورة أولاً.";
 
 
 
 
 
 
 
 
 
31
  return;
32
  }
33
 
34
- // تحويل الصورة إلى base64
35
- const file = fileInput.files[0];
36
- const reader = new FileReader();
37
- reader.onload = async function() {
38
- const base64Image = reader.result.split(',')[1]; // فقط الجزء base64
39
- // تجهيز البيانات للإرسال
40
- const params = new URLSearchParams();
41
- params.append('image', base64Image);
42
- params.append('language', 'ara'); // لو عايز لغة معينة غيرها (مثلاً eng, fra...)
 
 
43
 
44
- try {
45
- const response = await fetch('https://ocr-image-to-text-multilingual.p.rapidapi.com/imagetotext', {
46
- method: 'POST',
47
- headers: {
48
- 'x-rapidapi-key': '32769fb369mshfdf6f5e28e26674p1f3764jsn2a31085a1fc7',
49
- 'x-rapidapi-host': 'ocr-image-to-text-multilingual.p.rapidapi.com',
50
- 'Content-Type': 'application/x-www-form-urlencoded'
51
- },
52
- body: params
53
- });
54
 
55
- const data = await response.json();
56
- // عرض النص الناتج
57
- if (data.text) {
58
- resultDiv.textContent = data.text;
59
- } else {
60
- resultDiv.textContent = "لم يتم التعرف على نص في الصورة.";
61
- }
62
- } catch (error) {
63
- resultDiv.textContent = "حدث خطأ: " + error;
64
- }
65
- };
66
- reader.readAsDataURL(file);
67
  });
68
  </script>
69
  </body>
 
2
  <html lang="ar" dir="rtl">
3
  <head>
4
  <meta charset="UTF-8">
5
+ <title>تحليل صورة بالذكاء الاصطناعي</title>
6
  <style>
7
  body { font-family: Arial; margin: 40px; }
8
  label, button { display: block; margin-top: 15px; }
 
10
  </style>
11
  </head>
12
  <body>
13
+ <h2>تحليل صورة بالذكاء الاصطناعي</h2>
14
+ <form id="vision-form">
15
+ <label>رابط الصورة (أو ارفع صورة بالأسفل):
16
+ <input type="url" id="image-url" placeholder="https://example.com/image.jpg">
17
  </label>
18
+ <label>أو ارفع صورة:
19
+ <input type="file" id="image-input" accept="image/*">
20
+ </label>
21
+ <button type="submit">حلل الصورة</button>
22
  </form>
23
  <div id="result"></div>
24
 
25
  <script>
26
+ async function uploadToImgur(file) {
27
+ // رفع الصورة على Imgur للحصول على رابط مباشر
28
+ const formData = new FormData();
29
+ formData.append('image', file);
30
+ const res = await fetch('https://api.imgur.com/3/image', {
31
+ method: 'POST',
32
+ headers: { Authorization: 'Client-ID 54621f8d4e61e67' }, // استخدم ID مجاني من imgur.com
33
+ body: formData
34
+ });
35
+ const data = await res.json();
36
+ if (data.data && data.data.link) return data.data.link;
37
+ throw new Error("تعذر رفع الصورة");
38
+ }
39
+
40
+ document.getElementById('vision-form').addEventListener('submit', async function(event) {
41
  event.preventDefault();
42
  const resultDiv = document.getElementById('result');
43
+ resultDiv.textContent = "جاري التحليل...";
44
+
45
+ let imageUrl = document.getElementById('image-url').value.trim();
46
 
47
+ // لو مفيش رابط صورة والمستخدم رفع صورة من الجهاز
48
  const fileInput = document.getElementById('image-input');
49
+ if (!imageUrl && fileInput.files.length) {
50
+ try {
51
+ imageUrl = await uploadToImgur(fileInput.files[0]);
52
+ } catch (err) {
53
+ resultDiv.textContent = "خطأ في رفع الصورة: " + err.message;
54
+ return;
55
+ }
56
+ }
57
+
58
+ if (!imageUrl) {
59
+ resultDiv.textContent = "من فضلك ضع رابط صورة أو ارفع صورة من جهازك.";
60
  return;
61
  }
62
 
63
+ // تجهيز body بصيغة JSON
64
+ const body = JSON.stringify({
65
+ messages: [{
66
+ role: "user",
67
+ content: [
68
+ { type: "text", text: "what's in the image" },
69
+ { type: "image", url: imageUrl }
70
+ ]
71
+ }],
72
+ web_access: false
73
+ });
74
 
75
+ try {
76
+ const response = await fetch('https://chatgpt-vision1.p.rapidapi.com/matagvision2', {
77
+ method: 'POST',
78
+ headers: {
79
+ 'x-rapidapi-key': '091dbfbf0emsh6606eb2165cfd97p191257jsnd017aa229e09',
80
+ 'x-rapidapi-host': 'chatgpt-vision1.p.rapidapi.com',
81
+ 'Content-Type': 'application/json'
82
+ },
83
+ body: body
84
+ });
85
 
86
+ const data = await response.json();
87
+ resultDiv.textContent = JSON.stringify(data, null, 2);
88
+ } catch (error) {
89
+ resultDiv.textContent = "حدث خطأ: " + error;
90
+ }
 
 
 
 
 
 
 
91
  });
92
  </script>
93
  </body>