Advertisement
amt

Merge Locally

amt
Oct 18th, 2021 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Check out, review, and merge locally
  2. Step 1. Fetch and check out the branch for this merge request
  3.  
  4. git fetch origin
  5. git checkout -b "prod" "origin/prod"
  6. Step 2. Review the changes locally
  7.  
  8. Step 3. Merge the branch and fix any conflicts that come up
  9.  
  10. git fetch origin
  11. git checkout "master"
  12. git merge --no-ff "prod"
  13. Step 4. Push the result of the merge to GitLab
  14.  
  15. git push origin "master"
  16. Tip: You can also checkout merge requests locally by following these guidelines
  17. */
  18.  
  19.  
  20. /* tslint: disable */
  21. import * as XLSX from "xlsx";
  22. import React, { useState } from "react";
  23. import axios from "axios";
  24.  
  25. export default function FileUpload(props) {
  26.   const [selectedExcel, setSelectedExcel] = useState<File | null>(null);
  27.  
  28.   const [jsonForExcel, setJsonForExcel] = useState([]);
  29.  
  30.   const [excelUploadPostResponse, setExcelUploadPostResponse] = useState([]);
  31.  
  32.   const [existingFileCount, setExistingFileCount] = useState(0);
  33.  
  34.   const [jsonForCsv, setJsonForCsv] = useState([]);
  35.  
  36.   function excelUpload( event ) {
  37.     const selectedExcel =  event.target.files[0]
  38.     setSelectedExcel(selectedExcel);
  39.     console.log(selectedExcel);
  40.  
  41.     setExcelUploadPostResponse([]);
  42.  
  43.     const fileReader = new FileReader();
  44.  
  45.     fileReader.readAsBinaryString(selectedExcel as Blob);
  46.  
  47.     fileReader.onload = () => {
  48.  
  49.       const binaryExcel = fileReader.result;
  50.       console.log("binaryExcel", binaryExcel);
  51.  
  52.       const workbook = XLSX.read(binaryExcel, { type: "binary" });
  53.       console.log("workbook", workbook);
  54.  
  55.       if (workbook.SheetNames.length === 1) {
  56.  
  57.         const data = XLSX.utils.sheet_to_json(
  58.           workbook.Sheets[workbook.SheetNames[0]]
  59.         );
  60.         setJsonForExcel(data);
  61.         console.log(data);
  62.  
  63.       } else {
  64.  
  65.         const data = XLSX.utils.sheet_to_json(
  66.           workbook.Sheets["Output (2)"]
  67.         );
  68.         setJsonForExcel(data);
  69.         console.log(data);
  70.       }
  71.     };
  72.  
  73.     if (event && event.target && event.target.value) {
  74.       event.target.value = "";
  75.     }
  76.   }
  77.  
  78.   function clearUpload(): void {
  79.     setSelectedExcel(null);
  80.     setJsonForExcel([]);
  81.   }
  82.  
  83.   function triggerUpload(): void {
  84.     if (jsonForExcel.length) {
  85.       setExcelUploadPostResponse([]);
  86.       postExcel();
  87.     }
  88.   }
  89.  
  90.   async function postExcel(): Promise<void> {
  91.     try {
  92.       const uploadResponse = await axios.post(
  93.         "https://google.com/v1/excel_upload",
  94.         jsonForExcel
  95.       );
  96.       if (uploadResponse && uploadResponse.data && uploadResponse.data.length > 0) {
  97.         setExcelUploadPostResponse(uploadResponse.data);
  98.         uploadResponse.data.forEach((element) => {
  99.           if (element.Message) {
  100.             setExistingFileCount(
  101.               (prevFileCount) => prevFileCount + 1
  102.             );
  103.           }
  104.         });
  105.       } else {
  106.       }
  107.       // setLoading(false);
  108.     } catch (e) {
  109.       // setLoading(false);
  110.       // throw new Error("There was an error on user authentication");
  111.     }
  112.   }
  113.  
  114.   const openFile = function(event) {
  115.     let input = event.target;
  116.     let reader = new FileReader();
  117.     reader.onload = function() {
  118.       let text = reader.result;
  119.       console.log(text);
  120.       const lines = text.split("\n");
  121.       console.log(lines);
  122.  
  123.       let csvData = [];
  124.       for (let count = 0; count < lines.length; count++) {
  125.         const rowContent = lines[count].split(",");
  126.         console.log(rowContent);
  127.         csvData.push(rowContent);
  128.       }
  129.       console.log(csvData);
  130.       setJsonForCsv(csvData);
  131.     };
  132.     reader.readAsText(input.files[0]);
  133.   }
  134.  
  135.   return (
  136.     <>
  137.     <input
  138.       type="file"
  139.       id="fileInput"
  140.       className="drop-zoon__file-input"
  141.       accept=".xls,.xlsx"
  142.       onChange={excelUpload}
  143.     />
  144.  
  145.     <button
  146.       onClick={triggerUpload}
  147.       id="triggerUpload"
  148.     >
  149.       Trigger Build
  150.     </button>
  151.  
  152.     <button
  153.       aria-label="delete"
  154.       style={{ height: "40px" }}
  155.       onClick={clearUpload}
  156.     >
  157.       Clear
  158.     </button>
  159.       <div>
  160.         <br />
  161.         {excelUploadPostResponse.map((files) => {
  162.           if (!files.hasOwnProperty("Error"))
  163.             return (
  164.               <div>
  165.                     {files.hasOwnProperty("file") && (
  166.                       <div>
  167.                         {`File Name: `} <b>{`${files.file}`}</b>
  168.                       </div>
  169.                     )}
  170.                 <br />
  171.               </div>
  172.             );
  173.           return <></>;
  174.         })}
  175.       </div>
  176.  
  177.       {jsonForExcel && (
  178.       <>
  179.         <table
  180.           aria-label="simple table"
  181.           style={{
  182.             boxShadow:
  183.               "0px 2px 1px -1px rgb(0 0 0 / 20%), 0px 1px 1px 0px rgb(0 0 0 / 14%), 0px 1px 3px 0px rgb(0 0 0 / 12%)",
  184.             borderRadius: "5px",
  185.           }}
  186.         >
  187.           <thead>
  188.             <tr
  189.               style={{
  190.                 background: "#edf5f3",
  191.               }}
  192.             >
  193.               {jsonForExcel &&
  194.                 jsonForExcel.length > 0 &&
  195.                 Object.keys(jsonForExcel[0]).map(
  196.                   (row) => <th key={row}>{row}</th>
  197.                 )}
  198.             </tr>
  199.           </thead>
  200.           <tbody>
  201.             {jsonForExcel &&
  202.               jsonForExcel.length > 0 &&
  203.               jsonForExcel.map((row, index) => (
  204.                 <tr key={`tablerow${index}`}>
  205.                   {Object.keys(row).map((key, index) => (
  206.                     <td
  207.                       scope="row"
  208.                       key={`${row[key]}trow${index}`}
  209.                     >
  210.                       {row[key]}
  211.                     </td>
  212.                   ))}
  213.                 </tr>
  214.               ))}
  215.           </tbody>
  216.         </table>
  217.         <br />
  218.         <br />
  219.         <br />
  220.         <br />
  221.         <br />
  222.       </>
  223.     )}
  224.  
  225. <input type='file' accept='.csv' onChange={(event) => openFile(event)} />
  226. {jsonForCsv && (
  227.       <>
  228.         <table
  229.           aria-label="simple table"
  230.           style={{
  231.             boxShadow:
  232.               "0px 2px 1px -1px rgb(0 0 0 / 20%), 0px 1px 1px 0px rgb(0 0 0 / 14%), 0px 1px 3px 0px rgb(0 0 0 / 12%)",
  233.             borderRadius: "5px",
  234.           }}
  235.         >
  236.           {/* <thead>
  237.             <tr
  238.               style={{
  239.                 background: "#edf5f3",
  240.               }}
  241.             >
  242.               {jsonForCsv &&
  243.                 jsonForCsv.length > 0 &&
  244.                 jsonForCsv[0].map(
  245.                   (row) => <th key={row}>{row}</th>
  246.                 )}
  247.             </tr>
  248.           </thead> */}
  249.           <tbody>
  250.             {jsonForCsv &&
  251.               jsonForCsv.length > 0 &&
  252.               jsonForCsv.map((row, index) => (
  253.                 <tr key={`tablerow${index}`}>
  254.                   {row.map((key, index) => (
  255.                     <td
  256.                       scope="row"
  257.                       key={`${row[key]}trow${index}`}
  258.                     >
  259.                       {key}
  260.                     </td>
  261.                   ))}
  262.                 </tr>
  263.               ))}
  264.           </tbody>
  265.         </table>
  266.         <br />
  267.         <br />
  268.         <br />
  269.         <br />
  270.         <br />
  271.       </>
  272.     )}
  273.     </>
  274.   );
  275. }
  276.  
  277.  
  278. /*
  279. how to identify a face in uploaded image and replace with a smiley in react
  280.  
  281. To identify a face in an uploaded image and replace it with a smiley in a React application, you can use a combination of facial detection libraries like Face API or OpenCV.js for face detection and canvas manipulation to overlay the smiley.
  282.  
  283. Here’s a step-by-step guide:
  284.  
  285. Steps:
  286. Install Dependencies: Use a face detection library such as face-api.js:
  287.  
  288.  
  289. npm install face-api.js
  290. Setup File Upload and Image Rendering: Allow users to upload an image and render it on a <canvas>.
  291.  
  292. Detect Faces in the Image: Use face-api.js to detect faces in the uploaded image.
  293.  
  294. Overlay Smiley: Draw smiley faces on top of the detected face regions using the <canvas> API.
  295.  
  296. Example Code tsx:
  297.  
  298. */
  299.  
  300. import React, { useRef, useState } from "react";
  301. import * as faceapi from "face-api.js";
  302.  
  303. const FaceToSmiley = () => {
  304.   const [loading, setLoading] = useState(false);
  305.   const fileInputRef = useRef<HTMLInputElement | null>(null);
  306.   const canvasRef = useRef<HTMLCanvasElement | null>(null);
  307.   const imgRef = useRef<HTMLImageElement | null>(null);
  308.  
  309.   // Load models when the component mounts
  310.   React.useEffect(() => {
  311.     const loadModels = async () => {
  312.       setLoading(true);
  313.       await faceapi.nets.ssdMobilenetv1.loadFromUri("/models"); // Path to models folder
  314.       await faceapi.nets.faceLandmark68Net.loadFromUri("/models");
  315.       setLoading(false);
  316.     };
  317.     loadModels();
  318.   }, []);
  319.  
  320.   const handleFileUpload = async (e: React.ChangeEvent<HTMLInputElement>) => {
  321.     const file = e.target.files?.[0];
  322.     if (file && imgRef.current) {
  323.       imgRef.current.src = URL.createObjectURL(file);
  324.     }
  325.   };
  326.  
  327.   const handleImageLoaded = async () => {
  328.     if (!imgRef.current || !canvasRef.current) return;
  329.  
  330.     const img = imgRef.current;
  331.     const canvas = canvasRef.current;
  332.  
  333.     // Detect faces in the image
  334.     const detections = await faceapi.detectAllFaces(img);
  335.  
  336.     // Set canvas size to match the image
  337.     canvas.width = img.width;
  338.     canvas.height = img.height;
  339.  
  340.     // Draw the image on the canvas
  341.     const ctx = canvas.getContext("2d");
  342.     if (ctx) {
  343.       ctx.drawImage(img, 0, 0, img.width, img.height);
  344.  
  345.       // Replace each detected face with a smiley
  346.       detections.forEach((detection) => {
  347.         const box = detection.box;
  348.         const smiley = "😊";
  349.  
  350.         // Draw smiley over detected face region
  351.         ctx.font = `${box.width}px Arial`;
  352.         ctx.textAlign = "center";
  353.         ctx.textBaseline = "middle";
  354.         ctx.fillText(smiley, box.x + box.width / 2, box.y + box.height / 2);
  355.       });
  356.     }
  357.   };
  358.  
  359.   return (
  360.     <div>
  361.       {loading && <p>Loading face detection models...</p>}
  362.       <input
  363.         type="file"
  364.         accept="image/*"
  365.         ref={fileInputRef}
  366.         onChange={handleFileUpload}
  367.       />
  368.       <div style={{ position: "relative" }}>
  369.         <img
  370.           ref={imgRef}
  371.           onLoad={handleImageLoaded}
  372.           alt="uploaded"
  373.           style={{ display: "none" }}
  374.         />
  375.         <canvas ref={canvasRef} />
  376.       </div>
  377.     </div>
  378.   );
  379. };
  380.  
  381. export default FaceToSmiley;
  382. /*
  383. Explanation:
  384. Face API Models:
  385.  
  386. The example uses face-api.js for face detection.
  387. The models are loaded from the /models directory (you can download them here).
  388. File Upload:
  389.  
  390. Users upload an image via an <input> element.
  391. The image is loaded into an invisible <img> element for processing.
  392. Canvas Rendering:
  393.  
  394. The image is drawn on a <canvas> element for manipulation.
  395. The smiley is drawn at each detected face's coordinates using the fillText method.
  396. Overlay Smiley:
  397.  
  398. Each detected face's bounding box is replaced by a smiley using the bounding box dimensions to set the smiley's size and position.
  399. Additional Notes:
  400. Model Path: Ensure the /models folder is accessible in your public directory. It should contain the .bin files for face-api.js.
  401.  
  402. Improvement Ideas:
  403.  
  404. Allow resizing of smileys for better fit.
  405. Add multiple smiley options for fun customization.
  406. Include error handling for when no faces are detected.
  407. With this implementation, users can upload an image, and all faces will be replaced with smileys dynamically!
  408. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement