├── README.md
├── Sentimental_Analysis.ipynb
├── main code
└── object_recognition_for_pre_trained_model.ipynb
/README.md:
--------------------------------------------------------------------------------
1 | # -Implement-a-simple-calculator.-in-java
2 | The calculator is implemented using basic Java constructs such as classes, methods, and control statements.
3 |
--------------------------------------------------------------------------------
/main code:
--------------------------------------------------------------------------------
1 | import java.awt.event.*;
2 | import java.awt.*;
3 | import javax.swing.*;
4 | class Calculator extends JFrame implements ActionListener
5 | {
6 | JButton b10,b11,b12,b13,b14,b15;
7 | JButton b[]=new JButton[10];
8 | int i,r,n1,n2;
9 | JTextField res;
10 | char op;
11 | public Calculator()
12 | {
13 | super("Calulator");
14 | setLayout(new BorderLayout());
15 | JPanel p=new JPanel();
16 | p.setLayout(new GridLayout(4,4));
17 | for(int i=0;i<=9;i++)
18 | {
19 | b[i]=new JButton(i+"");
20 | p.add(b[i]);
21 | b[i].addActionListener(this);
22 | }
23 | b10=new JButton("+");
24 | p.add(b10);
25 | b10.addActionListener(this);
26 | b11=new JButton("-");
27 | p.add(b11);
28 | b11.addActionListener(this);
29 | b12=new JButton("*");
30 | p.add(b12);
31 | b12.addActionListener(this);
32 | b13=new JButton("/");
33 | p.add(b13);
34 | b13.addActionListener(this);
35 | b14=new JButton("=");
36 | p.add(b14);
37 | b14.addActionListener(this);
38 | b15=new JButton("C");
39 | p.add(b15);
40 | b15.addActionListener(this);
41 | res=new JTextField(10);
42 | add(p,BorderLayout.CENTER);
43 | add(res,BorderLayout.NORTH);
44 | setVisible(true);
45 | setSize(200,200);
46 | }
47 | public void actionPerformed(ActionEvent ae)
48 | {
49 | JButton pb=(JButton)ae.getSource();
50 | if(pb==b15)
51 | {
52 | r=n1=n2=0;
53 | res.setText("");
54 | }
55 | else
56 | if(pb==b14)
57 | {
58 | n2=Integer.parseInt(res.getText());
59 | eval();
60 | res.setText(""+r);
61 | }
62 | else
63 | {
64 | boolean opf=false;
65 | if(pb==b10)
66 | { op='+';
67 | opf=true;
68 | }
69 | if(pb==b11)
70 | { op='-';opf=true;}
71 | if(pb==b12)
72 | { op='*';opf=true;}
73 | if(pb==b13)
74 | { op='/';opf=true;}
75 | if(opf==false)
76 | {
77 | for(i=0;i<10;i++)
78 | {
79 | if(pb==b[i])
80 | {
81 | String t=res.getText();
82 | t+=i;
83 | res.setText(t);
84 | }
85 | }
86 | }
87 | else
88 | {
89 | n1=Integer.parseInt(res.getText());
90 | res.setText("");
91 | }
92 | }
93 | }
94 | int eval()
95 | {
96 | switch(op)
97 | {
98 | case '+': r=n1+n2; break;
99 | case '-': r=n1-n2; break;
100 | case '*': r=n1*n2; break;
101 | case '/': r=n1/n2; break;
102 |
103 | }
104 | return 0;
105 | }
106 | public static void main(String arg[])
107 | {
108 | new Calculator();
109 | }
110 | }
111 |
112 |
--------------------------------------------------------------------------------
/object_recognition_for_pre_trained_model.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "id": "view-in-github",
7 | "colab_type": "text"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": null,
16 | "id": "ff19884e",
17 | "metadata": {
18 | "scrolled": true,
19 | "id": "ff19884e",
20 | "outputId": "17aa5262-2652-4b45-a1f5-2f74d9f802de"
21 | },
22 | "outputs": [
23 | {
24 | "name": "stdout",
25 | "output_type": "stream",
26 | "text": [
27 | "Matching region at (0, 0) with score: -1706.7018022280058\n"
28 | ]
29 | },
30 | {
31 | "ename": "OSError",
32 | "evalue": "cannot write mode F as JPEG",
33 | "output_type": "error",
34 | "traceback": [
35 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
36 | "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)",
37 | "File \u001b[1;32m~\\anaconda3\\lib\\site-packages\\PIL\\JpegImagePlugin.py:630\u001b[0m, in \u001b[0;36m_save\u001b[1;34m(im, fp, filename)\u001b[0m\n\u001b[0;32m 629\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 630\u001b[0m rawmode \u001b[38;5;241m=\u001b[39m \u001b[43mRAWMODE\u001b[49m\u001b[43m[\u001b[49m\u001b[43mim\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmode\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m 631\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
38 | "\u001b[1;31mKeyError\u001b[0m: 'F'",
39 | "\nThe above exception was the direct cause of the following exception:\n",
40 | "\u001b[1;31mOSError\u001b[0m Traceback (most recent call last)",
41 | "Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 60\u001b[0m template \u001b[38;5;241m=\u001b[39m load_image(template_path)\n\u001b[0;32m 62\u001b[0m \u001b[38;5;66;03m# Perform template matching\u001b[39;00m\n\u001b[1;32m---> 63\u001b[0m best_match, best_score \u001b[38;5;241m=\u001b[39m \u001b[43mmatch_template\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimage\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtemplate\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 65\u001b[0m \u001b[38;5;66;03m# Display the result\u001b[39;00m\n\u001b[0;32m 66\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m best_match:\n",
42 | "Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36mmatch_template\u001b[1;34m(image, template)\u001b[0m\n\u001b[0;32m 48\u001b[0m best_score \u001b[38;5;241m=\u001b[39m score\n\u001b[0;32m 49\u001b[0m best_match \u001b[38;5;241m=\u001b[39m (x, y)\n\u001b[1;32m---> 50\u001b[0m \u001b[43msave_sub_image\u001b[49m\u001b[43m(\u001b[49m\u001b[43mimage_array\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43mtmpl_h\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtmpl_w\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mbest_match_region.jpg\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 52\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m best_match, best_score\n",
43 | "Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36msave_sub_image\u001b[1;34m(image_array, top_left, size, file_name)\u001b[0m\n\u001b[0;32m 17\u001b[0m h, w \u001b[38;5;241m=\u001b[39m size\n\u001b[0;32m 18\u001b[0m sub_image \u001b[38;5;241m=\u001b[39m image_array[y:y\u001b[38;5;241m+\u001b[39mh, x:x\u001b[38;5;241m+\u001b[39mw]\n\u001b[1;32m---> 19\u001b[0m \u001b[43mImage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfromarray\u001b[49m\u001b[43m(\u001b[49m\u001b[43msub_image\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msave\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile_name\u001b[49m\u001b[43m)\u001b[49m\n",
44 | "File \u001b[1;32m~\\anaconda3\\lib\\site-packages\\PIL\\Image.py:2212\u001b[0m, in \u001b[0;36mImage.save\u001b[1;34m(self, fp, format, **params)\u001b[0m\n\u001b[0;32m 2209\u001b[0m fp \u001b[38;5;241m=\u001b[39m builtins\u001b[38;5;241m.\u001b[39mopen(filename, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mw+b\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m 2211\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m-> 2212\u001b[0m \u001b[43msave_handler\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfp\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfilename\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 2213\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[0;32m 2214\u001b[0m \u001b[38;5;66;03m# do what we can to clean up\u001b[39;00m\n\u001b[0;32m 2215\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m open_fp:\n",
45 | "File \u001b[1;32m~\\anaconda3\\lib\\site-packages\\PIL\\JpegImagePlugin.py:632\u001b[0m, in \u001b[0;36m_save\u001b[1;34m(im, fp, filename)\u001b[0m\n\u001b[0;32m 630\u001b[0m rawmode \u001b[38;5;241m=\u001b[39m RAWMODE[im\u001b[38;5;241m.\u001b[39mmode]\n\u001b[0;32m 631\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m--> 632\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot write mode \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mim\u001b[38;5;241m.\u001b[39mmode\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m as JPEG\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n\u001b[0;32m 634\u001b[0m info \u001b[38;5;241m=\u001b[39m im\u001b[38;5;241m.\u001b[39mencoderinfo\n\u001b[0;32m 636\u001b[0m dpi \u001b[38;5;241m=\u001b[39m [\u001b[38;5;28mround\u001b[39m(x) \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m info\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdpi\u001b[39m\u001b[38;5;124m\"\u001b[39m, (\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m0\u001b[39m))]\n",
46 | "\u001b[1;31mOSError\u001b[0m: cannot write mode F as JPEG"
47 | ]
48 | }
49 | ],
50 | "source": [
51 | "from PIL import Image\n",
52 | "import numpy as np\n",
53 | "\n",
54 | "def load_image(image_path):\n",
55 | " return Image.open(image_path).convert('L') # Convert to grayscale\n",
56 | "\n",
57 | "def image_to_array(image):\n",
58 | " return np.array(image)\n",
59 | "\n",
60 | "def normalize_image(image_array):\n",
61 | " mean = np.mean(image_array)\n",
62 | " std = np.std(image_array)\n",
63 | " return (image_array - mean) / std\n",
64 | "\n",
65 | "def save_sub_image(image_array, top_left, size, file_name):\n",
66 | " y, x = top_left\n",
67 | " h, w = size\n",
68 | " sub_image = image_array[y:y+h, x:x+w]\n",
69 | " Image.fromarray(sub_image).save(file_name)\n",
70 | "\n",
71 | "def match_template(image, template):\n",
72 | " image_array = normalize_image(image_to_array(image))\n",
73 | " template_array = normalize_image(image_to_array(template))\n",
74 | "\n",
75 | " img_h, img_w = image_array.shape\n",
76 | " tmpl_h, tmpl_w = template_array.shape\n",
77 | "\n",
78 | " best_match = None\n",
79 | " best_score = -float('inf') # For NCC, higher values are better\n",
80 | "\n",
81 | " template_mean = np.mean(template_array)\n",
82 | " template_std = np.std(template_array)\n",
83 | "\n",
84 | " for y in range(img_h - tmpl_h + 1):\n",
85 | " for x in range(img_w - tmpl_w + 1):\n",
86 | " sub_image = image_array[y:y+tmpl_h, x:x+tmpl_w]\n",
87 | " sub_image_mean = np.mean(sub_image)\n",
88 | " sub_image_std = np.std(sub_image)\n",
89 | "\n",
90 | " norm_sub_image = (sub_image - sub_image_mean) / sub_image_std\n",
91 | " norm_template = (template_array - template_mean) / template_std\n",
92 | "\n",
93 | " score = np.sum(norm_sub_image * norm_template)\n",
94 | " print(f\"Matching region at ({x}, {y}) with score: {score}\")\n",
95 | "\n",
96 | " # Save the best matching region for visualization\n",
97 | " if score > best_score:\n",
98 | " best_score = score\n",
99 | " best_match = (x, y)\n",
100 | " save_sub_image(image_array, (y, x), (tmpl_h, tmpl_w), 'best_match_region.jpg')\n",
101 | "\n",
102 | " return best_match, best_score\n",
103 | "\n",
104 | "# Provide the paths to your image and template\n",
105 | "image_path = \"C:\\\\Users\\\\Lenovo\\\\Downloads\\\\download.jpeg\"\n",
106 | "template_path = \"C:\\\\Users\\\\Lenovo\\\\Downloads\\\\download (1).jpeg\"\n",
107 | "\n",
108 | "# Load the image and template\n",
109 | "image = load_image(image_path)\n",
110 | "template = load_image(template_path)\n",
111 | "\n",
112 | "# Perform template matching\n",
113 | "best_match, best_score = match_template(image, template)\n",
114 | "\n",
115 | "# Display the result\n",
116 | "if best_match:\n",
117 | " print(f\"Best match found at: {best_match} with score: {best_score}\")\n",
118 | "else:\n",
119 | " print(\"No match found\")\n"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": null,
125 | "id": "6237d922",
126 | "metadata": {
127 | "id": "6237d922",
128 | "outputId": "723d31f5-bf5c-4176-f909-1ab78912bd33"
129 | },
130 | "outputs": [
131 | {
132 | "name": "stdout",
133 | "output_type": "stream",
134 | "text": [
135 | "Image size: (275, 183)\n",
136 | "Template size: (275, 183)\n"
137 | ]
138 | }
139 | ],
140 | "source": [
141 | "print(f\"Image size: {image.size}\")\n",
142 | "print(f\"Template size: {template.size}\")\n"
143 | ]
144 | }
145 | ],
146 | "metadata": {
147 | "kernelspec": {
148 | "display_name": "Python 3 (ipykernel)",
149 | "language": "python",
150 | "name": "python3"
151 | },
152 | "language_info": {
153 | "codemirror_mode": {
154 | "name": "ipython",
155 | "version": 3
156 | },
157 | "file_extension": ".py",
158 | "mimetype": "text/x-python",
159 | "name": "python",
160 | "nbconvert_exporter": "python",
161 | "pygments_lexer": "ipython3",
162 | "version": "3.9.12"
163 | },
164 | "colab": {
165 | "provenance": [],
166 | "include_colab_link": true
167 | }
168 | },
169 | "nbformat": 4,
170 | "nbformat_minor": 5
171 | }
--------------------------------------------------------------------------------
|